| | |
| | | import android.bluetooth.BluetoothGattCharacteristic; |
| | | import android.bluetooth.BluetoothGattService; |
| | | import android.content.Intent; |
| | | import android.content.ServiceConnection; |
| | | import android.os.Binder; |
| | | import android.os.Handler; |
| | | import android.os.IBinder; |
| | |
| | | |
| | | @Override |
| | | public void onCreate() { |
| | | System.out.println("chenqi onCreate service" + MainApp.theApp.bleManager); |
| | | if (MainApp.theApp.bleManager != null) { |
| | | bleManager = MainApp.theApp.bleManager; |
| | | } else { |
| | | bleManager = new BleManager(MainApp.theApp); |
| | | MainApp.theApp.bleManager = bleManager; |
| | | } |
| | | bleManager.setMtu(128);//给外部接口设置数据传输大小 |
| | | // bleManager.setMtu(128);//给外部接口设置数据传输大小 |
| | | bleManager.enableBluetooth(); |
| | | } |
| | | |
| | | @Override |
| | | public void onDestroy() { |
| | | super.onDestroy(); |
| | | System.out.println("chenqi onDestroy service "); |
| | | bleManager = null; |
| | | mCallback = null; |
| | | mCallback2 = null; |
| | |
| | | return mBinder; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void unbindService(ServiceConnection conn) { |
| | | System.out.println("chenqi unbindService service"); |
| | | super.unbindService(conn); |
| | | } |
| | | |
| | | @Override |
| | | public boolean onUnbind(Intent intent) { |
| | | bleManager.closeBluetoothGatt(); |
| | | System.out.println("chenqi unbinding service"); |
| | | closeConnect(); |
| | | return super.onUnbind(intent); |
| | | } |
| | | |
| | | |
| | | public class BluetoothBinder extends Binder { |
| | | public BluetoothService getService() { |
| | |
| | | mCallback.onConnecting(); |
| | | } |
| | | |
| | | System.out.println("chenqi blename old = "+ MainApp.theApp.sharedPreferencesUtil.getBLE()); |
| | | System.out.println("chenqi blename old = " + MainApp.theApp.sharedPreferencesUtil.getBLE()); |
| | | |
| | | System.out.println("chenqi blename = "+ scanResult.getDevice().getAddress()); |
| | | System.out.println("chenqi blename = " + scanResult.getDevice().getAddress()); |
| | | |
| | | openBlueTooth(); |
| | | |
| | |
| | | |
| | | @Override |
| | | public void onDisConnected(BluetoothGatt gatt, int status, BleException exception) { |
| | | System.out.println("chenqi blueTooth connect state -> onDisConnected"); |
| | | System.out.println("chenqi blueTooth connect state -> onDisConnected" + exception); |
| | | runOnMainThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | |
| | | public void scanAndConnect5(String mac) { |
| | | resetInfo(); |
| | | |
| | | System.out.println("chenqi mCallback" + mCallback); |
| | | |
| | | if (mCallback != null) { |
| | | mCallback.onStartScan(); |
| | | } |
| | |
| | | |
| | | @Override |
| | | public void onConnectError(BleException exception) { |
| | | System.out.println("chenqi chenqi" + exception.getDescription()) ; |
| | | runOnMainThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | |
| | | |
| | | @Override |
| | | public void onDisConnected(BluetoothGatt gatt, int status, BleException exception) { |
| | | System.out.println("chenqi onConnectError " + exception.getDescription()); |
| | | runOnMainThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | |
| | | } |
| | | |
| | | public void closeConnect() { |
| | | bleManager.closeBluetoothGatt(); |
| | | if (bleManager != null) |
| | | bleManager.closeBluetoothGatt(); |
| | | } |
| | | |
| | | |