1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
| package xxxxxxxxxxxxxx;
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.BluetoothManager;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
class Ble
{
static interface BleObserver
{
public abstract void onBleConnected();
public abstract void onBleDisconnected();
public abstract void onBleReceiveText(String s, int i);
public abstract void onBleTimeout();
}
private static final int BLE_TIMEOUT = 4000;
private static final int REQUEST_TYPE_CONNECT = 1;
private BluetoothAdapter bAdapter;
private BluetoothGatt bGatt;
private BluetoothManager bManager;
private BluetoothGatt boardGatt;
private Context context;
private Set deviceSet;
private Set differDeviceSet;
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
private static final String CHARACTERISTIC_ID = "442f1571-8a00-9a28-cbe1-e1d4212d53eb";
private static final String CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000-8000-00805f9b34fb";
private static final String SERVICE_ID = "442f1570-8a00-9a28-cbe1-e1d4212d53eb";
private StringBuilder sb;
final Ble this$0;
public void onCharacteristicChanged(BluetoothGatt bluetoothgatt, BluetoothGattCharacteristic bluetoothgattcharacteristic)
{
String s = bluetoothgattcharacteristic.getStringValue(0).trim();
kawalog((new StringBuilder("onCharacteristicChanged:")).append(s).toString());
sb.append(s);
if (!s.endsWith("@"))
{
observer.onBleReceiveText(sb.toString(), 1);
return;
} else
{
observer.onBleReceiveText(sb.toString(), 0);
sb = new StringBuilder();
return;
}
}
public void onCharacteristicRead(BluetoothGatt bluetoothgatt, BluetoothGattCharacteristic bluetoothgattcharacteristic, int i)
{
kawalog((new StringBuilder("onCharacteristicRead:")).append(i).toString());
}
public void onConnectionStateChange(BluetoothGatt bluetoothgatt, int i, int j)
{
kawalog((new StringBuilder("onConnectionStateChangea:")).append(i).toString());
kawalog((new StringBuilder("newState:")).append(j).toString());
if (j == 2)
{
kawalog("connected");
bGatt.discoverServices();
} else
if (j == 0)
{
kawalog("disconnected");
if (boardGatt == bluetoothgatt)
{
Boolean boolean1 = isConnected;
disConnect();
if (boolean1.booleanValue())
{
(new Thread(new Runnable() {
final _cls1 this$1;
public void run()
{
try
{
Thread.sleep(500L);
startScan();
return;
}
catch (InterruptedException interruptedexception)
{
interruptedexception.printStackTrace();
}
}
{
this$1 = _cls1.this;
super();
}
})).start();
return;
}
}
}
}
public void onServicesDiscovered(BluetoothGatt bluetoothgatt, int i)
{
kawalog((new StringBuilder("onServicesDiscovered:")).append(i).toString());
Iterator iterator = bluetoothgatt.getServices().iterator();
BluetoothGattCharacteristic bluetoothgattcharacteristic;
label0:
do
{
BluetoothGattService bluetoothgattservice;
do
{
if (!iterator.hasNext())
{
kawalog("\u9055\u3046\u30C7\u30D0\u30A4\u30B9");
differDeviceSet.add(bGatt.getDevice());
nextDevice();
return;
}
bluetoothgattservice = (BluetoothGattService)iterator.next();
kawalog((new StringBuilder("service.getUuid().toString():")).append(bluetoothgattservice.getUuid().toString()).toString());
} while (bluetoothgattservice == null || bluetoothgattservice.getUuid() == null || !bluetoothgattservice.getUuid().toString().equals("442f1570-8a00-9a28-cbe1-e1d4212d53eb"));
Iterator iterator1 = bluetoothgattservice.getCharacteristics().iterator();
do
{
if (!iterator1.hasNext())
{
continue label0;
}
bluetoothgattcharacteristic = (BluetoothGattCharacteristic)iterator1.next();
} while (!bluetoothgattcharacteristic.getUuid().toString().equalsIgnoreCase("442f1571-8a00-9a28-cbe1-e1d4212d53eb"));
break;
} while (true);
BluetoothGattDescriptor bluetoothgattdescriptor;
if (bluetoothgatt.setCharacteristicNotification(bluetoothgattcharacteristic, true))
{
kawalog("okNotification");
boardGatt = bluetoothgatt;
bAdapter.stopLeScan(scanCallback);
connected();
} else
{
kawalog("status: failNotification");
}
bluetoothgattdescriptor = bluetoothgattcharacteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
bluetoothgattdescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothgatt.writeDescriptor(bluetoothgattdescriptor);
}
{
this$0 = Ble.this;
super();
sb = new StringBuilder();
}
};
private Boolean isActive;
private Boolean isConnected;
private BleObserver observer;
private int requestType;
private final android.bluetooth.BluetoothAdapter.LeScanCallback scanCallback = new android.bluetooth.BluetoothAdapter.LeScanCallback() {
final Ble this$0;
public void onLeScan(BluetoothDevice bluetoothdevice, int i, byte abyte0[])
{
kawalog("onLeScan");
if (bluetoothdevice.getName() == null)
{
kawalog("nullDevice");
return;
}
kawalog((new StringBuilder("onLeScan:")).append(bluetoothdevice.getName()).toString());
kawalog((new StringBuilder("scanRecord:")).append(abyte0.length).toString());
kawalog("connectGatt");
if (!differDeviceSet.contains(bluetoothdevice))
{
deviceSet.add(bluetoothdevice);
}
if (bGatt != null)
{
kawalog("\u691C\u7D22\u4E2D");
return;
} else
{
nextDevice();
return;
}
}
{
this$0 = Ble.this;
super();
}
};
private Timer timer;
public Ble(Context context1, BleObserver bleobserver)
{
isConnected = Boolean.valueOf(false);
isActive = Boolean.valueOf(false);
deviceSet = new HashSet();
differDeviceSet = new HashSet();
context = context1;
observer = bleobserver;
bManager = (BluetoothManager)context1.getSystemService("bluetooth");
bAdapter = bManager.getAdapter();
}
private void closeGatt()
{
if (bGatt != null)
{
kawalog("bGattClose");
bGatt.disconnect();
bGatt.close();
bGatt = null;
}
}
private void connected()
{
stopTimer();
isConnected = Boolean.valueOf(true);
observer.onBleConnected();
}
private void kawalog(String s)
{
Log.d("kawa", (new StringBuilder("kawalog:")).append(s).toString());
}
private void nextDevice()
{
if (deviceSet.isEmpty())
{
return;
} else
{
closeGatt();
final BluetoothDevice device = (BluetoothDevice)deviceSet.iterator().next();
kawalog((new StringBuilder("nextDevice:")).append(device.getName()).toString());
deviceSet.remove(device);
(new Handler(context.getMainLooper())).post(new Runnable() {
final Ble this$0;
private final BluetoothDevice val$device;
public void run()
{
bGatt = device.connectGatt(context, false, gattCallback);
}
{
this$0 = Ble.this;
device = bluetoothdevice;
super();
}
});
return;
}
}
private void onTime()
{
Log.d("kawalog", "onTime");
isConnected = Boolean.valueOf(false);
observer.onBleTimeout();
closeGatt();
stopScan();
}
private void startScan()
{
isActive = Boolean.valueOf(true);
if (isConnected.booleanValue())
{
kawalog("\u63A5\u7D9A\u6E08\u307F");
observer.onBleConnected();
stopTimer();
return;
}
startTimer();
closeGatt();
deviceSet = new HashSet();
differDeviceSet = new HashSet();
kawalog((new StringBuilder("isEnable:")).append(bAdapter.isEnabled()).toString());
if (!bAdapter.isEnabled())
{
bAdapter.enable();
}
bAdapter.startLeScan(scanCallback);
}
private void startTimer()
{
stopTimer();
Log.d("kawalog", "startTimer");
timer = new Timer();
timer.schedule(new TimerTask() {
final Ble this$0;
public void run()
{
onTime();
}
{
this$0 = Ble.this;
super();
}
}, 4000L);
}
private void stopScan()
{
kawalog("stopScan");
stopTimer();
isActive = Boolean.valueOf(false);
if (bAdapter == null)
{
return;
} else
{
bAdapter.cancelDiscovery();
bAdapter.stopLeScan(scanCallback);
return;
}
}
private void stopTimer()
{
kawalog("stopTimer");
if (timer != null)
{
timer.cancel();
timer = null;
}
}
public void disConnect()
{
kawalog("-------disconnect-------");
stopTimer();
stopScan();
if (!isConnected.booleanValue())
{
return;
} else
{
isConnected = Boolean.valueOf(false);
observer.onBleDisconnected();
closeGatt();
return;
}
}
public void onPause()
{
bAdapter.stopLeScan(scanCallback);
}
public void onResume()
{
kawalog("---------------onResume!!!!!!");
if (isActive.booleanValue()) goto _L2; else goto _L1
_L1:
kawalog("not active");
if (!isConnected.booleanValue()) goto _L2; else goto _L3
_L3:
observer.onBleConnected();
stopTimer();
_L5:
return;
_L2:
if (!isConnected.booleanValue())
{
kawalog("startScan");
startScan();
return;
}
if (true) goto _L5; else goto _L4
_L4:
}
public void tryConnect()
{
requestType = 1;
stopScan();
startScan();
}
} |
Partager