我一直试图弄清楚为什么我的代码不想找到我的MSP432 SensorHub示例(MSP432 + CC2650 BoosterPack + Sensor BoosterPack堆栈)的服务。将在下面显示代码和logcat。
public class SensorOutput extends AppCompatActivity {
private static final String TAG = "Data Output";
private BluetoothGatt mBluetoothGatt;
private ProgressDialog mProgress;
private TextView mTemp;
/* Temp Service */
private static final UUID TEMP_SERVICE = UUID.fromString("f000aa00-0451-4000-b000-000000000000");
private static final UUID TEMP_DATA = UUID.fromString("f000aa01-0451-4000-b000-000000000000");
private static final UUID TEMP_CONFIG = UUID.fromString("f000aa02-0451-4000-b000-000000000000");
/* Humidity Service */
public static final UUID HUMIDITY_SERVICE = UUID.fromString("f000aa20-0451-4000-b000-000000000000");
public static final UUID HUMIDITY_DATA = UUID.fromString("f000aa21-0451-4000-b000-000000000000");
public static final UUID HUMIDITY_CONFIG = UUID.fromString("f000aa22-0451-4000-b000-000000000000");
/* Barometer Service */
public static final UUID BAROMETER_SERVICE = UUID.fromString("f000aa40-0451-4000-b000-000000000000");
public static final UUID BAROMETER_DATA = UUID.fromString("f000aa41-0451-4000-b000-000000000000");
public static final UUID BAROMETER_CONFIG = UUID.fromString("f000aa42-0451-4000-b000-000000000000");
/* Optical Service */
public static final UUID OPTIC_SERVICE = UUID.fromString("f000aa70-0451-4000-b000-000000000000");
public static final UUID OPTIC_DATA = UUID.fromString("f000aa71-0451-4000-b000-000000000000");
public static final UUID OPTIC_CONFIG = UUID.fromString("f000aa72-0451-4000-b000-000000000000");
/* Movement Service */
public static final UUID MOVEMENT_SERVICE = UUID.fromString("f000aa80-0451-4000-b000-000000000000");
public static final UUID MOVEMENT_DATA = UUID.fromString("f000aa81-0451-4000-b000-000000000000");
public static final UUID MOVEMENT_CONFIG = UUID.fromString("f000aa82-0451-4000-b000-000000000000");
/* Client Configuration Descriptor */
private static final UUID CONFIG_DESCRIPTOR = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data);
mTemp = (TextView) findViewById(R.id.temperatureData);
mProgress = new ProgressDialog(this);
mProgress.setIndeterminate(true);
mProgress.setCancelable(false);
BluetoothDevice device = getIntent().getExtras().getParcelable("Bluetooth_Device");
connect(device);
}
public void connect(BluetoothDevice device) {
if (mBluetoothGatt == null) {
mBluetoothGatt = device.connectGatt(SensorOutput.this, false, mGattCallback);
mBluetoothGatt.discoverServices();
}
}
public final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
/* State Machine Tracking */
private int mState = 0;
private void reset() { mState = 0; }
private void advance() { mState++; }
/*
* Send an enable command to each sensor by writing a configuration
* characteristic. This is specific to the SensorTag to keep power
* low by disabling sensors you aren't using.
*/
private void enableNextSensor(BluetoothGatt gatt) {
BluetoothGattCharacteristic characteristic;
switch (mState) {
case 0:
Log.d(TAG, "Enabling temperature");
characteristic = gatt.getService(TEMP_SERVICE)
.getCharacteristic(TEMP_CONFIG);
characteristic.setValue(new byte[] {0x01});
break;
case 1:
Log.d(TAG, "Enabling optic");
characteristic = gatt.getService(OPTIC_SERVICE)
.getCharacteristic(OPTIC_CONFIG);
characteristic.setValue(new byte[] {0x01});
break;
case 2:
Log.d(TAG, "Enabling barometer");
characteristic = gatt.getService(BAROMETER_SERVICE)
.getCharacteristic(BAROMETER_CONFIG);
characteristic.setValue(new byte[] {0x01});
break;
case 3:
Log.d(TAG, "Enabling humidity");
characteristic = gatt.getService(HUMIDITY_SERVICE)
.getCharacteristic(HUMIDITY_CONFIG);
characteristic.setValue(new byte[] {0x01});
break;
case 4:
Log.d(TAG, "Enabling movement");
characteristic = gatt.getService(MOVEMENT_SERVICE)
.getCharacteristic(MOVEMENT_CONFIG);
characteristic.setValue(new byte[] {0x01});
break;
default:
mHandler.sendEmptyMessage(MSG_DISMISS);
Log.i(TAG, "All Sensors Enabled");
return;
}
gatt.writeCharacteristic(characteristic);
}
/*
* Read the data characteristic's value for each sensor explicitly
*/
private void readNextSensor(BluetoothGatt gatt) {
BluetoothGattCharacteristic characteristic;
switch (mState) {
case 0:
Log.d(TAG, "Reading temperature");
characteristic = gatt.getService(TEMP_SERVICE)
.getCharacteristic(TEMP_DATA);
break;
case 1:
Log.d(TAG, "Reading optic");
characteristic = gatt.getService(OPTIC_SERVICE)
.getCharacteristic(OPTIC_DATA);
break;
case 2:
Log.d(TAG, "Reading barometer");
characteristic = gatt.getService(BAROMETER_SERVICE)
.getCharacteristic(BAROMETER_DATA);
break;
case 3:
Log.d(TAG, "Reading humidity");
characteristic = gatt.getService(HUMIDITY_SERVICE)
.getCharacteristic(HUMIDITY_DATA);
break;
case 4:
Log.d(TAG, "Reading movement");
characteristic = gatt.getService(MOVEMENT_SERVICE)
.getCharacteristic(MOVEMENT_DATA);
break;
default:
mHandler.sendEmptyMessage(MSG_DISMISS);
Log.i(TAG, "All Sensors Enabled");
return;
}
gatt.readCharacteristic(characteristic);
}
/*
* Enable notification of changes on the data characteristic for each sensor
* by writing the ENABLE_NOTIFICATION_VALUE flag to that characteristic's
* configuration descriptor.
*/
private void setNotifyNextSensor(BluetoothGatt gatt) {
BluetoothGattCharacteristic characteristic;
switch (mState) {
case 0:
Log.d(TAG, "Set notify temperature");
characteristic = gatt.getService(TEMP_SERVICE)
.getCharacteristic(TEMP_DATA);
break;
case 1:
Log.d(TAG, "Set notify optic");
characteristic = gatt.getService(OPTIC_SERVICE)
.getCharacteristic(OPTIC_DATA);
break;
case 2:
Log.d(TAG, "Set notify barometer");
characteristic = gatt.getService(BAROMETER_SERVICE)
.getCharacteristic(BAROMETER_DATA);
break;
case 3:
Log.d(TAG, "Set notify humidity");
characteristic = gatt.getService(HUMIDITY_CONFIG)
.getCharacteristic(HUMIDITY_DATA);
break;
case 4:
Log.d(TAG, "Set notify movement");
characteristic = gatt.getService(MOVEMENT_CONFIG)
.getCharacteristic(MOVEMENT_DATA);
break;
default:
mHandler.sendEmptyMessage(MSG_DISMISS);
Log.i(TAG, "All Sensors Enabled");
return;
}
//Enable local notifications
gatt.setCharacteristicNotification(characteristic, true);
//Enabled remote notifications
BluetoothGattDescriptor desc = characteristic.getDescriptor(CONFIG_DESCRIPTOR);
desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(desc);
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.d(TAG, "Connection State Change: " + status + " -> " + connectionState(newState));
if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {
/*
* Once successfully connected, we must next discover all the services on the
* device before we can read and write their characteristics.
*/
gatt.discoverServices();
mHandler.sendMessage(Message.obtain(null, MSG_PROGRESS, "Discovering Services..."));
} else if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) {
/*
* If at any point we disconnect, send a message to clear the weather values
* out of the UI
*/
mHandler.sendEmptyMessage(MSG_CLEAR);
} else if (status != BluetoothGatt.GATT_SUCCESS) {
/*
* If there is a failure at any stage, simply disconnect
*/
gatt.disconnect();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d(TAG, "Services Discovered: " + status);
mHandler.sendMessage(Message.obtain(null, MSG_PROGRESS, "Enabling Sensors..."));
/*
* With services discovered, we are going to reset our state machine and start
* working through the sensors we need to enable
*/
reset();
enableNextSensor(gatt);
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
//For each read, pass the data up to the UI thread to update the display
if (TEMP_DATA.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_TEMP, characteristic));
}
if (OPTIC_DATA.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_OPTIC, characteristic));
}
if (BAROMETER_DATA.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_BAROMETER, characteristic));
}
if (HUMIDITY_DATA.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_HUMIDITY, characteristic));
}
if (MOVEMENT_DATA.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_MOVEMENT, characteristic));
}
//After reading the initial value, next we enable notifications
setNotifyNextSensor(gatt);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
//After writing the enable flag, next we read the initial value
readNextSensor(gatt);
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
/*
* After notifications are enabled, all updates from the device on characteristic
* value changes will be posted here. Similar to read, we hand these up to the
* UI thread to update the display.
*/
if (TEMP_DATA.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_TEMP, characteristic));
}
if (OPTIC_DATA.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_OPTIC, characteristic));
}
if (BAROMETER_DATA.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_BAROMETER, characteristic));
}
if (HUMIDITY_DATA.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_HUMIDITY, characteristic));
}
if (MOVEMENT_DATA.equals(characteristic.getUuid())) {
mHandler.sendMessage(Message.obtain(null, MSG_MOVEMENT, characteristic));
}
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
//Once notifications are enabled, we move to the next sensor and start over with enable
advance();
enableNextSensor(gatt);
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
Log.d(TAG, "Remote RSSI: "+rssi);
}
private String connectionState(int status) {
switch (status) {
case BluetoothProfile.STATE_CONNECTED:
return "Connected";
case BluetoothProfile.STATE_DISCONNECTED:
return "Disconnected";
case BluetoothProfile.STATE_CONNECTING:
return "Connecting";
case BluetoothProfile.STATE_DISCONNECTING:
return "Disconnecting";
default:
return String.valueOf(status);
}
}
};
}
Log cat:
D / BluetoothGatt:onClientConnParamsChanged() - Device = A0:E6:F8:BA:2D:XX interval = 6 status = 0
D / BluetoothGatt:onSearchComplete()= Device = A0:E6:F8:BA:2D:XX 状态= 0
D /数据输出:发现的服务:0
D /数据输出:启用温度
D /数据输出:读取温度
D / BluetoothGatt:onClientConnParamsChanged() - 设备= A0:E6:F8:BA:2D:XX间隔= 39状态= 0