实现一个android服务,通过蓝牙读取Arduino上的按钮传感器输入
我正在尝试将Arduino 101上的按钮传感器连接到Android应用程序 其想法是实现一个应用程序,该应用程序连接到Arduino上的蓝牙硬件按钮,然后在后台运行一个服务,不断监听蓝牙广播。如果点击按钮,蓝牙信号将通过Arduino传输,服务应捕获该信号并打开应用程序 我使用Android的蓝牙通用属性配置文件(GATT)官方文档将任意数据从Arduino传输到后台服务。我的服务能够连接到蓝牙信号并断开连接,但无法检测到按钮的点击 谁能帮帮我吗 这是蓝牙侦听器服务的代码实现一个android服务,通过蓝牙读取Arduino上的按钮传感器输入,android,bluetooth,android-sensors,arduino-uno,android-hardware,Android,Bluetooth,Android Sensors,Arduino Uno,Android Hardware,我正在尝试将Arduino 101上的按钮传感器连接到Android应用程序 其想法是实现一个应用程序,该应用程序连接到Arduino上的蓝牙硬件按钮,然后在后台运行一个服务,不断监听蓝牙广播。如果点击按钮,蓝牙信号将通过Arduino传输,服务应捕获该信号并打开应用程序 我使用Android的蓝牙通用属性配置文件(GATT)官方文档将任意数据从Arduino传输到后台服务。我的服务能够连接到蓝牙信号并断开连接,但无法检测到按钮的点击 谁能帮帮我吗 这是蓝牙侦听器服务的代码 private fi
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
};
private void broadcastUpdate(final String action) {
final Intent intent = new Intent(action);
sendBroadcast(intent);
}
private void broadcastUpdate(final String action,
final BluetoothGattCharacteristic characteristic) {
final Intent intent = new Intent(action);
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for(byte byteChar : data)
stringBuilder.append(String.format("%02X ", byteChar));
System.out.println(stringBuilder);
intent.putExtra(EXTRA_DATA,"\n" + stringBuilder.toString());
}
sendBroadcast(intent);
}
以下是另一个服务中的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;
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
mConnected = false;
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
// Show all the supported services and characteristics on the user interface.
displayGattServices(mBluetoothLeService.getSupportedGattServices());
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
System.out.println(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
}
}
};
嘿,连接正常吗?你能在arduino和android之间发送数据吗?您能串行打印从Android传输的任何数据吗?是的,Arduino代码正在工作。我可以在Android活动中传输arduino的数据,但不能在服务中传输。嘿,连接正常吗?你能在arduino和android之间发送数据吗?您能串行打印从Android传输的任何数据吗?是的,Arduino代码正在工作。我可以在Android活动中传输arduino的数据,但不能在服务中传输。