BeneCheck 使用蓝牙模块 BLE 获取血糖尿酸胆固醇数据
李国帅 于2016-8-28
按说明书检查编码器,插入试纸,启动app,滴血检验,检测结果会通过notify的形式传递到蓝牙的接收回调
函数onCharacteristicChanged中。
package com.example.glucoseexample;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity {
private MainActivity thisContext;
private TextView device_info;
private String device_log;
private BluetoothAdapter btAdapt;
private BluetoothGatt mBluetoothGatt;
private BluetoothDevice mbtDev;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
thisContext = this;
btAdapt = BluetoothAdapter.getDefaultAdapter();
IntentFilter intent = new IntentFilter();
intent.addAction(BluetoothDevice.ACTION_FOUND);
intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(searchDevices, intent);
device_info = (TextView) findViewById(R.id.device_info);
if (btAdapt.getState() == BluetoothAdapter.STATE_OFF) {
device_log = "" + "蓝牙未打开\n";
} else if (btAdapt.getState() == BluetoothAdapter.STATE_ON) {
device_log = "" + "蓝牙已打开\n";
}
device_info.setText(device_log);
Log.e("ble", "startDiscovery");
btAdapt.startDiscovery();
}
private BroadcastReceiver searchDevices = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String str = device.getName() + "|" + device.getAddress();
if (str.indexOf("BeneCheck") != -1) {
String[] values = str.split("\\|");
String address = values[1];
Log.e("address", values[1]);
mbtDev = btAdapt.getRemoteDevice(address);
mBluetoothGatt = mbtDev.connectGatt(thisContext, true, mGattCallback);
}
}
}
};
@Override
protected void onDestroy() {
Log.e("ble", "onDestroy");
if (btAdapt != null && btAdapt.isDiscovering()) {
btAdapt.cancelDiscovery();
}
if (mBluetoothGatt != null && mBluetoothGatt.connect()) {
mBluetoothGatt.disconnect();
}
this.unregisterReceiver(searchDevices);
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
}
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic
characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
byte[] bVal = characteristic.getValue();
int base = (((bVal[11] & 0x0F) << 8) + (bVal[10] & 0xFF));
int comp = ((byte) (bVal[11] ^ 0xff) >> 4) + 1;
if ((bVal[11] & 0x80) == 0x80)
comp = (-1) * comp;
Double dVal = (double) (base * Math.pow(10, comp) * 1000);
String value = "特征值改变回调:";
if (bVal[1] == 0x41)// 血糖
{
value = value + " 血糖=" + dVal.toString();
} else if (bVal[1] == 0x51)// 尿酸
{
value = value + "尿酸=" + dVal.toString();
} else if (bVal[1] == 0x61)// 胆固醇
{
value = value + "胆固醇=" + dVal.toString();
}
Message msg = new Message();
msg.obj = value;
mHandler.sendMessage(msg);
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
Message msg = new Message();
msg.obj = "设备已连接";
mHandler.sendMessage(msg);
gatt.discoverServices();
btAdapt.cancelDiscovery();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Message msg = new Message();
msg.obj = "设备已断开";
mHandler.sendMessage(msg);
btAdapt.startDiscovery();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
String value = "发现服务回调--Gatt";
Message msg = new Message();
msg.obj = value;
mHandler.sendMessage(msg);
testGlucoseNotification();
}
}
private void testGlucoseNotification() {
String serviceUUID = "00001808-0000-1000-8000-00805F9B34FB";//
BluetoothGattService mBluetoothGattService =
mBluetoothGatt.getService(UUID.fromString(serviceUUID));
if (mBluetoothGattService != null) {
Log.i("ble", "Service characteristic UUID found: " +
mBluetoothGattService.getUuid().toString());
String characterUUID = "00002a18-0000-1000-8000-00805F9B34FB";//
BluetoothGattCharacteristic bgcMeasurement = mBluetoothGattService
.getCharacteristic(UUID.fromString(characterUUID));
mBluetoothGatt.setCharacteristicNotification(bgcMeasurement, true);
String descriptUUID = "00002902-0000-1000-8000-00805F9B34FB";//
BluetoothGattDescriptor descriptor =
bgcMeasurement.getDescriptor(UUID.fromString(descriptUUID));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
} else {
Log.i("ble", "Service characteristic not found for UUID: " + serviceUUID);
}
}
};
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
String strMsg = (String) msg.obj;
Log.d("ble", strMsg);
device_log = device_log + strMsg + "\n";
device_info.setText(device_log);
}
};
}
其他资料:
onetouch 血糖仪用户指南
benecheck 血糖仪 BLE 分析公式