无法连接到BLE设备setItemOnClickListener

时间:2018-09-07 17:13:27

标签: android bluetooth-lowenergy listviewitem background-service

我已经为设备创建了后台服务,以连接和读取广告包。 列表中显示了我在蓝牙低功耗扫描仪扫描后收到的设备。但是,每当我单击ListView项目时,即使在获取地址后运行服务,设备也不会连接。 我正在使用此方法连接设备

public boolean connect(final String address) {
        if (mBluetoothAdapter == null || address == null) {
            Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
            return false;
        }

        // Previously connected device.  Try to reconnect.
        if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
                && mBluetoothGatt != null) {
            Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
            if (mBluetoothGatt.connect()) {
                mConnectionState = STATE_CONNECTING;
                return true;
            } else {
                return false;
            }
        }

        final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        if (device == null) {
            Log.w(TAG, "Device not found.  Unable to connect.");
            Toast.makeText(this, "Unable to connect", Toast.LENGTH_SHORT).show();
            return false;
        }
        // We want to directly connect to the device, so we are setting the autoConnect
        // parameter to false.
        mBluetoothGatt = device.connectGatt(this, false, mGattCallBack);
        Log.d(TAG, "Trying to create a new connection.");
        mBluetoothDeviceAddress = address;
        mConnectionState = STATE_CONNECTING;
        return true;
    }

我尝试达到的方法是伙伴。在DeviceActivity中

Intent gattServiceIntent = new Intent(this, LeBluetoothService.class);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(context, "you clicked", Toast.LENGTH_SHORT).show();

                device = mLeDeviceListAdapter.getDevice(position);
                mDeviceAddress = device.getAddress();

                bindService(gattServiceIntent, mServiceConnection, BIND_ABOVE_CLIENT);




                mScanning = false;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    mBluetoothAdapter.getBluetoothLeScanner().stopScan(scanCallback);
                } else {

                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                }
                invalidateOptionsMenu();


//                mLeBluetoothService.connect(mDeviceAddress);


                //   Toast.makeText(context, "connected to: "+mDeviceAddress, Toast.LENGTH_SHORT).show();

            }
        });

对于自动服务连接,我正在使用以下代码。

 private final ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mLeBluetoothService = ((LeBluetoothService.LocalBinder) service).getService();
            if (!mLeBluetoothService.initialize()) {
                Toast.makeText(context, "Unable to initialize Bluetooth", Toast.LENGTH_SHORT).show();
                finish();

            }
            mLeBluetoothService.connect(mDeviceAddress);
            Toast.makeText(context, "Connected to " + mDeviceAddress, Toast.LENGTH_SHORT).show();


        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mLeBluetoothService = null;
        }
    };

当我尝试直接从列表视图进行连接时,它给了我Null指针异常。

1 个答案:

答案 0 :(得分:0)

尝试这个图书馆 https://github.com/Jasonchenlijian/FastBle 它为您提供了简单的方法