依然是菜菜鸟 2021-03-15 10:45 采纳率: 66.7%
浏览 369
已结题

Android studio BLE开发闪退,Android开发闪退原因

private void connectLeDevice(final String mac){
        //获取蓝牙适配器
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(bluetoothAdapter == null){
            Log.d("connectDevice","bluetoothAdapter is null");
            return;
        }
        //根据mac地址获取蓝牙设备实例对象,当然此处的blueDevcie也可以通过蓝牙扫描得到
        BluetoothDevice bluetoothDevice = bluetoothAdapter.getRemoteDevice(mac);
        if(bluetoothDevice == null){
            Log.d("connectDevice","the device is not exist");
            return;
        }
        gattCallback=new BluetoothGattCallback() {
            /**
             * 断开或连接 状态发生变化时调用
             * */
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                super.onConnectionStateChange(gatt, status, newState);
                Log.e(TAG,"onConnectionStateChange()");
                if (status==BluetoothGatt.GATT_SUCCESS){
                    //连接成功
                    if (newState== BluetoothGatt.STATE_CONNECTED){
                        Log.e(TAG,"连接成功");
                        //发现服务
                        gatt.discoverServices();
                    }else{
                        //断开连接之后的操作
                    }
                }else{
                    //连接失败
                    Log.e(TAG,"失败=="+status);
                    mBluetoothGatt.close();
                    mBluetoothGatt.disconnect();
                    isConnecting=false;
                }
            }
            /**
             * 发现设备(真正建立连接)
             * */
            @Override
            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                super.onServicesDiscovered(gatt, status);
                //直到这里才是真正建立了可通信的连接
                isConnecting=false;
                Log.e(TAG,"onServicesDiscovered()---建立连接");
                //获取初始化服务和特征值
                initServiceAndChara();
                //订阅通知
                mBluetoothGatt.setCharacteristicNotification(mBluetoothGatt
                        .getService(notify_UUID_service).getCharacteristic(notify_UUID_chara),true);

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run(){
                        bleListView.setVisibility(View.GONE);
                        operaView.setVisibility(View.VISIBLE);
                        tvSerBindStatus.setText("已连接");
                    }
                });
            }
            /**
             * 读操作的回调
             * */
            @Override
            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicRead(gatt, characteristic, status);
                Log.e(TAG,"onCharacteristicRead()");
            }
            /**
             * 写操作的回调
             * */
            @Override
            public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicWrite(gatt, characteristic, status);

                Log.e(TAG,"onCharacteristicWrite()  status="+status+",value="+HexUtil.encodeHexStr(characteristic.getValue()));
            }
            /**
             * 接收到硬件返回的数据
             * */
            @Override
            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
                super.onCharacteristicChanged(gatt, characteristic);
                Log.e(TAG,"onCharacteristicChanged()"+ Arrays.toString(characteristic.getValue()));
                final byte[] data=characteristic.getValue();
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        addText(tvResponse,bytes2hex(data));
                    }
                });

            }
        };
        //获取当前gatt服务下连接的设备
        if(mBluetoothGatt != null && mBluetoothGatt.getConnectedDevices() != null){
            for(BluetoothDevice device : mBluetoothGatt.getConnectedDevices()){
                if(device.getAddress().equals(mac)){    // 如果当前遍历出的连接设备与我们需要连得设备是同一设备
                    mBluetoothGatt.disconnect();        //先去断开之前未正常断开的连接,解决连接133的问题
                }
            }
        }
        mBluetoothGatt.close();                         //释放Gatt服务
        mBluetoothGatt = null;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mBluetoothGatt = bluetoothDevice.connectGatt(getContext(),false,gattCallback,TRANSPORT_LE);
        }else {
            mBluetoothGatt = bluetoothDevice.connectGatt(getContext(),false,gattCallback);
        }
    }
bleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d(TAG,"已点击ListView");
                if (isScaning){
                    stopScanDevice();
                }
                if (!isConnecting){
                    isConnecting=true;
                    BluetoothDevice bluetoothDevice= mDatas.get(position);

                    //连接设备
                    tvSerBindStatus.setText("连接中");
                    connectLeDevice(bluetoothDevice.getAddress());
                }
                Log.d(TAG,"退出ListVIew点击事件");
            }
        });

做一个BLE蓝牙开发,能够扫描显示设备到ListView,但是点击设备进行连接立马闪退

  • 写回答

2条回答 默认 最新

  • 简单吗 2021-03-15 17:00
    关注

    错误日志发出来

    评论

报告相同问题?

悬赏问题

  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了