AfterYoung 2021-09-08 10:37 采纳率: 81.8%
浏览 57
已结题

有一个BLE的DEMO,要加入到一个蓝牙开门的项目中,现在在如何写获取扫描得到的蓝牙设备列表的函数这里卡住了,希望能够得到一些指点和帮助。


public class ScanDevices{



    private String SERVICESUUID = "0000ffe0-0000-1000-8000-00805f9b34fb";   //服务的UUID
    private String WRITEUUID = "0000ffe3-0000-1000-8000-00805f9b34fb";      //写入特征的UUID



    private BluetoothDevice mDevice;
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothGatt bluetoothGatt;
    private BluetoothGattService bluetoothGattService;
    private BluetoothGattCharacteristic writeCharacteristic;
    private final List<BluetoothDevice> BlueToothDevice_Info = new ArrayList<>();//蓝牙设备信息
    private Handler mHandler;
    private static Context mContext;


    public void setContext(Context context){
        mContext = context;
    }

    /*
    扫描设备
     */

    private void scanLeDevice(final boolean enable) {
        if (enable) {
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                  stopScan();
                }
            }, 5000); //5秒后停止搜索
            startScan();
        } else {
            stopScan();
        }
    }

    /*
    开始扫描设备
     */
    private void startScan(){
        mBluetoothAdapter.startLeScan(mLeScanCallback); //开始搜索
    }

    /*
    停止扫描设备
     */
    private void stopScan(){
        mBluetoothAdapter.stopLeScan(mLeScanCallback);//停止搜索
    }

    /**
     * 扫描设备回调
     */

    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback(){
        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
            if(device.getName() != null){
                ((Activity) mContext).runOnUiThread(new Runnable(){
                    @Override
                    public void run() {
                        if (!BlueToothDevice_Info.contains(device)){
                            BlueToothDevice_Info.add(device);
                        }
                    }
                });
                mDevice = mBluetoothAdapter.getRemoteDevice(device.toString());
                //第二个参数,是否重连
                bluetoothGatt = mDevice.connectGatt(mContext,false,bluetoothGattCallback);
            }
        }
    };

    /*
    蓝牙连接成功回调
     */
    private final BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {

        //连接状态改变时回调
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (newState == BluetoothProfile.STATE_CONNECTED){
                Log.d("onConnectionStateChange","连接成功");
                //发现服务
                gatt.discoverServices();
            }else if (newState == BluetoothProfile.STATE_DISCONNECTED){
                Log.d("onConnectionStateChange","断开连接");
                if (mDevice != null){
                    //关闭当前新的连接
                    gatt.close();
                }
            }
        }


        //写入成功回调函数

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS){
                Log.d("onCharacteristicWrite","写入成功");
            }
        }

        //UUID搜索成功回调

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            Log.d("onCharacteristicWrite","写入成功");
            if (status == BluetoothGatt.GATT_SUCCESS){
                List<BluetoothGattService> supportedGattServices = bluetoothGatt.getServices();
                for (int i = 0; i < supportedGattServices.size(); i++) {
                    Log.i("success", "1:BluetoothGattService UUID=:" + supportedGattServices.get(i).getUuid());
                    List<BluetoothGattCharacteristic> listGattCharacteristic = supportedGattServices.get(i).getCharacteristics();
                    for (int j = 0; j < listGattCharacteristic.size(); j++) {
                        Log.i("success", "2:   BluetoothGattCharacteristic UUID=:" + listGattCharacteristic.get(j).getUuid());
                    }
                }
            }else{
                Log.e("fail","onservicesdiscovered收到:" + status);
            }
            //设置serviceUUID
            bluetoothGattService = bluetoothGatt.getService(UUID.fromString(SERVICESUUID));
            //设置写入特征UUID
            writeCharacteristic = bluetoothGattService.getCharacteristic(UUID.fromString(WRITEUUID));
            //开启监听
            Log.d("uuidconnectsuccess","uuid连接成功");
        }

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            byte[] value = characteristic.getValue();

        }
    };

//如何将我扫描得到的BlueToothDevice_Info能够通过这个函数传递出去
    public List<SysScanResult> getSysResultList(){

    }

}

//SysScanResult是我写的一个List的实体类,代码如下
public class SysScanResult {
    public String ssid;
    public int rssi;

    public SysScanResult(String var1, int var2) {
        this.ssid = var1;
        this.rssi = var2;
    }

    public boolean equals(Object var1) {
        if (this == var1) {
            return true;
        } else if (var1 != null && this.getClass() == var1.getClass()) {
            SysScanResult var2 = (SysScanResult)var1;
            if (this.ssid != null) {
                return this.ssid.equals(var2.ssid);
            } else {
                return var2.ssid == null;
            }
        } else {
            return false;
        }
    }

    public int hashCode() {
        return this.ssid != null ? this.ssid.hashCode() : 0;
    }

    public String toString() {
        return "{ssid='" + this.ssid + '\'' + ", rssi=" + this.rssi + '}';
    }
}
  • 写回答

问题事件

  • 系统已结题 12月4日
  • 已采纳回答 11月26日
  • 创建了问题 9月8日

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来