首页 文章

android蓝牙BLE连接流程

提问于
浏览
2

蓝牙连接应用

我写了扫描设备的服务 .

当我获得设备时 onLeScan() callback 电话服务添加到列表并在上次可见时进行监控,并检查一些特征,现在所有应用等待用户交互 .

1)我应该在读取blutetoothGatt disconnect() or close()? 上的特征调用数据之后

现在,我打电话给 disconnect() ,设备已断开但突然再次通话后我收到了 onLeScan() 回拨

2)我应该在onConnectionStateChange中对连接问题作出反应,还是跳过它并在 onLeScan() 回调后接收设备后做一些?

1 回答

  • 0

    首先,连接到您的BLE .
    然后,使用以下代码发现您的服务 .

    private final BluetoothGattCallback mGattcallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status,
                                            int newState) {
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                mStatus = newState;
                mConnGatt.discoverServices();
                Toast.makeText(getApplicationContext(),"Device Connected", Toast.LENGTH_SHORT).show();
            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                mStatus = newState;
                runOnUiThread(new Runnable() {
                    public void run() { 
                 setInterval.setEnabled(false);
                    }
                });
            }
        }
    

    在callBack之后,将UUID设置为按钮 . 我正在使用setInterval按钮 .

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        for (BluetoothGattService service : gatt.getServices()) {
            if ((service == null) || (service.getUuid() == null)) {
                continue;
            }
            if (BleUuid.IR_SERVICE.equalsIgnoreCase(service
                    .getUuid().toString())) {
                runOnUiThread(new Runnable() {
                    public void run() {
                        setInterval.setEnabled(true);
                    }
    
                });
                setInterval.setTag(service
                        .getCharacteristic(UUID
                                .fromString(BleUuid.WRITE_TIME)));
            }
    
            runOnUiThread(new Runnable() {
                public void run() {
                    setProgressBarIndeterminateVisibility(false);
                }
            });
        }}
    

    然后,读写类

    @Override
        public void onCharacteristicRead(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                if (BleUuid.CHAR_MANUFACTURER_NAME_STRING
                        .equalsIgnoreCase(characteristic.getUuid().toString())) {
                    final String name = characteristic.getStringValue(0);
    
                    runOnUiThread(new Runnable() {
                        public void run() {
                            setInterval.setText(name);
                            setProgressBarIndeterminateVisibility(false);
                        }
                    });
                }
            }
        }
    
        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt,
                                          BluetoothGattCharacteristic characteristic, int status) {
    
            runOnUiThread(new Runnable() {
                public void run() {
                    setProgressBarIndeterminateVisibility(false);
                }
            });
        }
    };
    

    然后我使用了一个点击监听器将数据发送到BLE .

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setinterval:
                if ((v.getTag() != null)
                        && (v.getTag() instanceof BluetoothGattCharacteristic)) {
                    BluetoothGattCharacteristic ch = (BluetoothGattCharacteristic) v
                            .getTag();
                    String hr = String.valueOf(tp.getCurrentHour());
                    String min = String.valueOf(tp.getCurrentMinute());
                    String interval = new StringBuilder().append(hr).append(min).toString();
                    ch.setValue(interval);
                    if (mConnGatt.writeCharacteristic(ch)) {
                        setProgressBarIndeterminateVisibility(true);
                        Toast.makeText(getApplicationContext(),R.string.toast_interval +interval, Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }}
    

相关问题