蓝牙温度计在调试模式下连续发送数据,但在运行模式android studio中

时间:2018-03-01 05:12:51

标签: android android-studio bluetooth-lowenergy

我正在开发一款从蓝牙温度计中获取温度的Android应用程序。我按照谷歌开发者页面上的说明进行操作。

https://developer.android.com/guide/topics/connectivity/bluetooth-le.html

我可以扫描设备并连接到温度计。我正在使用以下github中的相同代码,只需要很少的修改就可以满足我的需求。

https://github.com/googlesamples/android-BluetoothLeGatt/

而不是使用心率服务和相关特征。我正在使用温度服务和特性。我在从github

下载的下一页上更改了这些值

机器人-BluetoothLeGatt /应用/ SRC /主/ JAVA / COM /示例/机器人/ bluetoothlegatt / SampleGattAttributes.java

我正在使用以下蓝牙温度计。

http://www.cooper-atkins.com/Products/Blue2/

这是我从COOPER-ATKINS获得的API文件。

https://drive.google.com/open?id=1eq93Qc6uy0Vv9KompukLuIUnJK4B-e-0

在第6页,我突出显示了我在github代码中替换的服务uuid​​s和特征uuids。

我已将修改过的项目的zip文件上传到

https://drive.google.com/open?id=1MMBX8IgX6ZbJPIiQnc3YG8WVexLQuO2x

在DeviceControlActivity.java中,我有BroadcastReceiver函数

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
            mConnected = true;
            updateConnectionState(R.string.connected);
            invalidateOptionsMenu();
        } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
            mConnected = false;
            updateConnectionState(R.string.disconnected);
            invalidateOptionsMenu();
            clearUI();
        } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {

            BluetoothGattService BatteryService = mBluetoothLeService.getSupportedGattServices().get(3);
            BluetoothGattCharacteristic batteryCharacteristic = BatteryService.getCharacteristic(UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb"));
            GetBLEData(batteryCharacteristic);

        } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {

            displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA), 0);

        } else if (BluetoothLeService.BATTERY_DATA_AVAILABLE.equals(action)) {

            displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA), 1);
            BluetoothGattService tempService = mBluetoothLeService.getSupportedGattServices().get(2);
            BluetoothGattCharacteristic tempCharacteristic = tempService.getCharacteristic(UUID.fromString("78544003-4394-4fc2-8cfd-be6a00aa701b"));
            GetBLEData(tempCharacteristic);
        }

    }
};

一旦发现设备,我得到BLE温度计的电池百分比,在收到电池百分比后,我打电话给具有温度特性的GetBLEData()。

private void GetBLEData(BluetoothGattCharacteristic characteristic){
        if (characteristic != null) {
            final int charaProp = characteristic.getProperties();
            if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
                // If there is an active notification on a characteristic, clear
                // it first so it doesn't update the data field on the user interface.
                if (mNotifyCharacteristic != null) {
                    mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, false);
                    mNotifyCharacteristic = null;
                }
                mBluetoothLeService.readCharacteristic(characteristic);
            }
            if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                mNotifyCharacteristic = characteristic;
                mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, true);
                Toast.makeText(this, "Test Working", Toast.LENGTH_LONG).show();
            }

        }
    }

我逐步调试GetBLEData()的每一行,同时调试我得到了温度数据的变化,但是如果代码运行我就不会收到更新。有人能帮助我理解我做错了什么吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

使用等待线程做了伎俩

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! TableViewDateCell
    cell.titleStatus.text = mainList[indexPath.row].statusName
    //tableView.isEditing = true
    //disable swipe to delete for cell with statusId == "12"
    if mainList[indexPath.row].statusId == "12" {
         //tableView.isEditing = false
        cell.selectionStyle = UITableViewCellSelectionStyle.gray
        cell.isUserInteractionEnabled = false

    }
 }


func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

}