【问题标题】:android Bluetooth BLE connection flow安卓蓝牙BLE连接流程
【发布时间】:2015-11-03 13:58:56
【问题描述】:

蓝牙连接应用程序

我已经编写了扫描设备的服务。

当我在onLeScan() 上获得设备时回调 电话服务添加到列表并在上次可见并检查某些特征时进行监控,而这一切现在都是等待用户交互的应用程序。

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

现在,我拨打disconnect(),设备已断开连接,但再次拨打后突然我在onLeScan()回调中收到它

2) 我应该在哪里对 onConnectionStateChange 中的连接问题做出反应或跳过它并在接收设备后做一些onLeScan() 回调?

【问题讨论】:

  • 发现?你是说扫描?

标签: android bluetooth bluetooth-lowenergy android-bluetooth


【解决方案1】:

首先,连接到您的 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;
    }}

【讨论】:

  • 我知道这不是一个准确的数据。但是这个例子可能会对你有所帮助。
猜你喜欢
  • 2014-08-31
  • 2013-07-19
  • 2011-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多