1212import android .bluetooth .BluetoothGattService ;
1313import android .bluetooth .BluetoothManager ;
1414import android .bluetooth .BluetoothProfile ;
15+ import android .content .BroadcastReceiver ;
1516import android .content .Context ;
1617import android .content .Intent ;
1718import android .content .IntentFilter ;
1819import android .os .Binder ;
19- import android .os .Handler ;
2020import android .os .IBinder ;
2121import android .support .v7 .app .NotificationCompat ;
2222import android .util .Log ;
@@ -49,15 +49,58 @@ public class BluetoothLeService extends Service {
4949 public static final int STATE_CONNECTING = 1 ;
5050 public static final int STATE_CONNECTED = 2 ;
5151
52- private Handler mHandler = new Handler ();
52+
53+ private final BroadcastReceiver messageReceiver = new BroadcastReceiver () {
54+ @ Override
55+ public void onReceive (Context context , Intent intent ) {
56+ if (mConnectionState != STATE_CONNECTED )
57+ return ;
58+
59+ final String action = intent .getAction ();
60+ if (Constants .ACTION_REQUEST_NAME_DATA .equals (action )) {
61+
62+ byte [] data = new byte [20 ];
63+ data [0 ] = (byte ) -86 ;
64+ data [1 ] = (byte ) 85 ;
65+ data [16 ] = (byte ) -101 ;
66+ data [17 ] = (byte ) 20 ;
67+ data [18 ] = (byte ) 90 ;
68+ data [19 ] = (byte ) 90 ;
69+ writeBluetoothGattCharacteristic (data );
70+ } else if (Constants .ACTION_REQUEST_SERIAL_DATA .equals (action )) {
71+
72+ byte [] data = new byte [20 ];
73+ data [0 ] = (byte ) -86 ;
74+ data [1 ] = (byte ) 85 ;
75+ data [16 ] = (byte ) 99 ;
76+ data [17 ] = (byte ) 20 ;
77+ data [18 ] = (byte ) 90 ;
78+ data [19 ] = (byte ) 90 ;
79+ writeBluetoothGattCharacteristic (data );
80+ } else if (Constants .ACTION_REQUEST_HORN .equals (action )) {
81+
82+ byte [] data = new byte [20 ];
83+ data [0 ] = (byte ) -86 ;
84+ data [1 ] = (byte ) 85 ;
85+ data [16 ] = (byte ) -120 ;
86+ data [17 ] = (byte ) 20 ;
87+ data [18 ] = (byte ) 90 ;
88+ data [19 ] = (byte ) 90 ;
89+ writeBluetoothGattCharacteristic (data );
90+ }
91+ }
92+ };
5393
5494 public static IntentFilter makeBluetoothUpdateIntentFilter () {
5595 final IntentFilter intentFilter = new IntentFilter ();
96+ intentFilter .addAction (Constants .ACTION_BLUETOOTH_CONNECTING );
5697 intentFilter .addAction (Constants .ACTION_BLUETOOTH_CONNECTED );
5798 intentFilter .addAction (Constants .ACTION_BLUETOOTH_DISCONNECTED );
5899 intentFilter .addAction (Constants .ACTION_BLUETOOTH_DATA_AVAILABLE );
59100 intentFilter .addAction (Constants .ACTION_WHEEL_DATA_AVAILABLE );
60101 intentFilter .addAction (Constants .ACTION_REQUEST_SERIAL_DATA );
102+ intentFilter .addAction (Constants .ACTION_REQUEST_NAME_DATA );
103+ intentFilter .addAction (Constants .ACTION_REQUEST_HORN );
61104 return intentFilter ;
62105 }
63106
@@ -84,7 +127,7 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
84127
85128 } else if (newState == BluetoothProfile .STATE_DISCONNECTED ) {
86129 mConnectionState = STATE_DISCONNECTED ;
87- Wheel . getInstance ( ).setConnectionState (false );
130+ (( WheelLog ) getApplicationContext () ).setConnectionState (false );
88131 LOGI ("Disconnected from GATT server." );
89132 broadcastUpdate (Constants .ACTION_BLUETOOTH_DISCONNECTED );
90133 }
@@ -120,7 +163,6 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {
120163 BluetoothGattDescriptor descriptor = characteristic .getDescriptor (UUID .fromString (BluetoothLeService .DESCRIPTER_UUID ));
121164 descriptor .setValue (BluetoothGattDescriptor .ENABLE_NOTIFICATION_VALUE );
122165 mBluetoothGatt .writeDescriptor (descriptor );
123- mConnectionState = BluetoothLeService .STATE_CONNECTED ;
124166 mConnectionState = STATE_CONNECTED ;
125167 return ;
126168 }
@@ -150,9 +192,7 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
150192// stringBuilder.append(String.format("%02X ", objArr));
151193// }
152194// LOGI("received data = " + stringBuilder);
153- int result = Wheel .getInstance ().decodeResponse (value );
154- if (result == Constants .REQUEST_SERIAL_DATA )
155- sendBroadcast (new Intent (Constants .ACTION_REQUEST_SERIAL_DATA ));
195+ ((WheelLog ) getApplicationContext ()).decodeResponse (value );
156196
157197 Intent intent = new Intent (Constants .ACTION_WHEEL_DATA_AVAILABLE );
158198 sendBroadcast (intent );
@@ -164,7 +204,7 @@ public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descri
164204 super .onDescriptorWrite (gatt , descriptor , status );
165205 LOGI ("onDescriptorWrite " + String .valueOf (status ));
166206 if (status == BluetoothGatt .GATT_SUCCESS ) {
167- Wheel . getInstance ( ).setConnectionState (true );
207+ (( WheelLog ) getApplicationContext () ).setConnectionState (true );
168208 broadcastUpdate (Constants .ACTION_BLUETOOTH_CONNECTED );
169209 }
170210 }
@@ -202,6 +242,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
202242 .setContentIntent (pendingIntent )
203243 .build ();
204244
245+ registerReceiver (messageReceiver , makeBluetoothUpdateIntentFilter ());
205246 startForeground (1 , notification );
206247
207248 return START_STICKY ;
@@ -263,6 +304,7 @@ public boolean connect(final String address) {
263304 Log .d (TAG , "Trying to use an existing mBluetoothGatt for connection." );
264305 if (mBluetoothGatt .connect ()) {
265306 mConnectionState = STATE_CONNECTING ;
307+ broadcastUpdate (Constants .ACTION_BLUETOOTH_CONNECTING );
266308 return true ;
267309 } else {
268310 return false ;
@@ -279,6 +321,7 @@ public boolean connect(final String address) {
279321 Log .d (TAG , "Trying to create a new connection." );
280322 mBluetoothDeviceAddress = address ;
281323 mConnectionState = STATE_CONNECTING ;
324+ broadcastUpdate (Constants .ACTION_BLUETOOTH_CONNECTING );
282325 return true ;
283326 }
284327
@@ -305,7 +348,6 @@ public void disconnect() {
305348 * released properly.
306349 */
307350 public void close () {
308- mHandler .removeCallbacksAndMessages (null );
309351
310352 if (mBluetoothGatt == null ) {
311353 return ;
0 commit comments