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

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
    关注

    错误日志发出来

    评论

报告相同问题?

悬赏问题

  • ¥15 提问一个关于vscode相关的环境配置问题,就是输入中文但是显示不出来,代码在idea可以显示中文,但在vscode不行,不知道怎么配置环境
  • ¥15 netcore使用PuppeteerSharp截图
  • ¥20 这张图页脚具体代码该怎么写?
  • ¥20 WPF MVVM模式 handycontrol 框架, hc:SearchBar 控件 Text="{Binding NavMenusKeyWords}" 绑定取不到值
  • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
  • ¥15 数字信号处理考试111
  • ¥15 allegro17.2生成bom表是空白的
  • ¥15 请问一下怎么打通CAN通讯
  • ¥20 如何在 rocky9.4 部署 CDH6.3.2?
  • ¥35 navicat将excel中的数据导入mysql出错