List of usage examples for android.bluetooth BluetoothProfile STATE_DISCONNECTED
int STATE_DISCONNECTED
To view the source code for android.bluetooth BluetoothProfile STATE_DISCONNECTED.
Click Source Link
From source file:Main.java
public static String getBleConnectStatus(int status) { switch (status) { case BluetoothProfile.STATE_DISCONNECTED: return "STATE_DISCONNECTED"; case BluetoothProfile.STATE_CONNECTING: return "STATE_CONNECTING"; case BluetoothProfile.STATE_CONNECTED: return "STATE_CONNECTED"; case BluetoothProfile.STATE_DISCONNECTING: return "STATE_DISCONNECTING"; default://from w w w .jav a2 s.c o m return "STATE_UNKNOWN: " + status; } }
From source file:fr.bmartel.android.bluetooth.connection.BluetoothDeviceConn.java
/** * Build Bluetooth device connection// ww w . ja v a2s . c o m * * @param address */ @SuppressLint("NewApi") public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) { this.deviceAddr = address; this.deviceName = deviceName; this.manager = manager; gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { String intentAction; if (newState == BluetoothProfile.STATE_CONNECTED) { intentAction = ActionFilterGatt.ACTION_GATT_CONNECTED; Log.i(TAG, "Connected to GATT server."); Log.i(TAG, "Attempting to start service discovery:" + gatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { connected = false; intentAction = ActionFilterGatt.ACTION_GATT_DISCONNECTED; Log.i(TAG, "Disconnected from GATT server."); manager.broadcastUpdate(ActionFilterGatt.ACTION_GATT_DISCONNECTED); if (BluetoothDeviceConn.this.gatt != null) { BluetoothDeviceConn.this.gatt.close(); } } } @Override // New services discovered public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { Runnable test = new Runnable() { @Override public void run() { //you can improve this by using reflection device = new DottiDevice(BluetoothDeviceConn.this); device.addInitListener(new IDeviceInitListener() { @Override public void onInit() { try { String name = ""; JSONObject object = new JSONObject(); object.put("address", getAddress()); object.put("deviceName", getDeviceName()); ArrayList<String> values = new ArrayList<String>(); values.add(object.toString()); connected = true; //when device is fully intitialized broadcast service discovery manager.broadcastUpdateStringList( ActionFilterGatt.ACTION_GATT_SERVICES_DISCOVERED, values); } catch (JSONException e) { e.printStackTrace(); } } }); device.init(); } }; Thread testThread = new Thread(test); testThread.start(); } else { Log.w(TAG, "onServicesDiscovered received: " + status); } } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { System.out.println("characteristic write received "); manager.getEventManager().set(); if (device != null) { device.notifyCharacteristicWriteReceived(characteristic); } } @Override // Result of a characteristic read operation public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { manager.getEventManager().set(); if (device != null) { device.notifyCharacteristicReadReceived(characteristic); } } @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { System.out.println("descriptor write received "); manager.getEventManager().set(); } @Override // Characteristic notification public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { System.out.println("descriptor change received "); if (device != null) { device.notifyCharacteristicChangeReceived(characteristic); } } }; }
From source file:fr.bmartel.android.notti.service.bluetooth.connection.BluetoothDeviceConn.java
/** * Build Bluetooth device connection/* ww w . java 2 s. c om*/ * * @param address */ @SuppressLint("NewApi") public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) { this.deviceAddr = address; this.deviceName = deviceName; this.manager = manager; gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { Log.i(TAG, "Connected to GATT server."); Log.i(TAG, "Attempting to start service discovery:" + gatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { connected = false; Log.i(TAG, "Disconnected from GATT server."); try { JSONObject object = new JSONObject(); object.put(BluetoothConst.DEVICE_ADDRESS, getAddress()); object.put(BluetoothConst.DEVICE_NAME, getDeviceName()); ArrayList<String> values = new ArrayList<String>(); values.add(object.toString()); //when device is fully intitialized broadcast service discovery manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCONNECTED, values); } catch (JSONException e) { e.printStackTrace(); } if (BluetoothDeviceConn.this.gatt != null) { BluetoothDeviceConn.this.gatt.close(); } } } @Override // New services discovered public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { Runnable test = new Runnable() { @Override public void run() { //you can improve this by using reflection device = new NottiDevice(BluetoothDeviceConn.this); device.addInitListener(new IDeviceInitListener() { @Override public void onInit() { try { JSONObject object = new JSONObject(); object.put(BluetoothConst.DEVICE_ADDRESS, getAddress()); object.put(BluetoothConst.DEVICE_NAME, getDeviceName()); ArrayList<String> values = new ArrayList<String>(); values.add(object.toString()); connected = true; //when device is fully intitialized broadcast service discovery manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_CONNECTED, values); } catch (JSONException e) { e.printStackTrace(); } } }); device.init(); } }; Thread testThread = new Thread(test); testThread.start(); } else { Log.w(TAG, "onServicesDiscovered received: " + status); } } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { manager.getEventManager().set(); if (device != null) { device.notifyCharacteristicWriteReceived(characteristic); } } @Override // Result of a characteristic read operation public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { manager.getEventManager().set(); if (device != null) { device.notifyCharacteristicReadReceived(characteristic); } } @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { manager.getEventManager().set(); } @Override // Characteristic notification public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { if (device != null) { device.notifyCharacteristicChangeReceived(characteristic); } } }; }
From source file:com.github.akinaru.bleanalyzer.bluetooth.connection.BluetoothDeviceConn.java
/** * Build Bluetooth device connection/* ww w . j a v a 2 s .c om*/ * * @param address */ @SuppressLint("NewApi") public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) { this.deviceAddr = address; this.deviceName = deviceName; this.manager = manager; gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { Log.i(TAG, "Connected to GATT server."); Log.i(TAG, "Attempting to start service discovery:" + gatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { connected = false; Log.i(TAG, "Disconnected from GATT server."); try { JSONObject object = new JSONObject(); object.put(JsonConstants.BT_ADDRESS, getAddress()); object.put(JsonConstants.BT_DEVICE_NAME, getDeviceName()); ArrayList<String> values = new ArrayList<String>(); values.add(object.toString()); //when device is fully intitialized broadcast service discovery manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCONNECTED, values); } catch (JSONException e) { e.printStackTrace(); } if (manager.getWaitingMap().containsKey(deviceAddr)) { manager.getWaitingMap().get(deviceAddr).cancel(true); manager.getWaitingMap().remove(deviceAddr); } if (BluetoothDeviceConn.this.gatt != null) { Log.i(TAG, "connection close clean"); BluetoothDeviceConn.this.gatt.close(); } } } @Override // New services discovered public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { Runnable test = new Runnable() { @Override public void run() { //you can improve this by using reflection device = new RfduinoDevice(BluetoothDeviceConn.this); device.addInitListener(new IDeviceInitListener() { @Override public void onInit() { try { JSONObject object = new JSONObject(); object.put(JsonConstants.BT_ADDRESS, getAddress()); object.put(JsonConstants.BT_DEVICE_NAME, getDeviceName()); ArrayList<String> values = new ArrayList<String>(); values.add(object.toString()); connected = true; //when device is fully intitialized broadcast service discovery manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_CONNECTED, values); } catch (JSONException e) { e.printStackTrace(); } } }); device.init(); } }; Thread testThread = new Thread(test); testThread.start(); } else { Log.w(TAG, "onServicesDiscovered received: " + status); } } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { //manager.getEventManager().set(); if (device != null) { device.notifyCharacteristicWriteReceived(characteristic); } } @Override // Result of a characteristic read operation public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { manager.getEventManager().set(); if (device != null) { Log.i(TAG, "onCharacteristicRead"); device.notifyCharacteristicReadReceived(characteristic); } } @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { manager.getEventManager().set(); } @Override // Characteristic notification public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { manager.getEventManager().set(); if (device != null) { Log.i(TAG, "onCharacteristicChanged"); device.notifyCharacteristicChangeReceived(characteristic); } } }; }
From source file:com.github.akinaru.roboticbuttonpusher.bluetooth.connection.BluetoothDeviceConn.java
/** * Build Bluetooth device connection// w w w. ja v a 2 s.c o m * * @param address */ @SuppressLint("NewApi") public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) { this.deviceAddr = address; this.deviceName = deviceName; this.manager = manager; gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { Log.v(TAG, "Connected to GATT server."); Log.v(TAG, "Attempting to start service discovery:" + gatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { connected = false; Log.v(TAG, "Disconnected from GATT server."); if (status != 133) { manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCONNECTED, new ArrayList<String>()); } else { manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_RETRY, new ArrayList<String>()); } if (manager.getWaitingMap().containsKey(deviceAddr)) { manager.getWaitingMap().get(deviceAddr).cancel(true); manager.getWaitingMap().remove(deviceAddr); } if (BluetoothDeviceConn.this.gatt != null) { Log.v(TAG, "connection close clean"); BluetoothDeviceConn.this.gatt.close(); } if (remove) { manager.getConnectionList().remove(deviceAddr); manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_REMOVED, new ArrayList<String>()); } } } @Override // New services discovered public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { Runnable test = new Runnable() { @Override public void run() { //you can improve this by using reflection device = new RfduinoDevice(BluetoothDeviceConn.this); device.addInitListener(new IDeviceInitListener() { @Override public void onInit() { try { JSONObject object = new JSONObject(); object.put(JsonConstants.BT_ADDRESS, getAddress()); object.put(JsonConstants.BT_DEVICE_NAME, getDeviceName()); ArrayList<String> values = new ArrayList<String>(); values.add(object.toString()); connected = true; //when device is fully intitialized broadcast service discovery manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_CONNECTED, values); } catch (JSONException e) { e.printStackTrace(); } } }); device.init(); } }; Thread testThread = new Thread(test); testThread.start(); } else { Log.w(TAG, "onServicesDiscovered received: " + status); } } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { manager.getEventManager().set(); if (device != null) { device.notifyCharacteristicWriteReceived(characteristic); } } @Override // Result of a characteristic read operation public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { manager.getEventManager().set(); if (device != null) { Log.v(TAG, "onCharacteristicRead"); device.notifyCharacteristicReadReceived(characteristic); } } @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { manager.getEventManager().set(); } @Override // Characteristic notification public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { manager.getEventManager().set(); if (device != null) { Log.v(TAG, "onCharacteristicChanged"); device.notifyCharacteristicChangeReceived(characteristic); } } }; }
From source file:org.physical_web.physicalweb.BluetoothSite.java
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED && status == gatt.GATT_SUCCESS) { Log.i(TAG, "Connected to GATT server"); mBluetoothGatt = gatt;/*from www . j av a 2s.c o m*/ if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { gatt.requestConnectionPriority(CONNECTION_PRIORITY_HIGH); gatt.requestMtu(505); } else { gatt.discoverServices(); } } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { Log.i(TAG, "Disconnected to GATT server"); // ensure progress dialog is removed and running is set false close(); } else if (status != gatt.GATT_SUCCESS) { Log.i(TAG, "Status is " + status); close(); } }
From source file:com.junkchen.blelib.sample.MultipleBleActivity.java
private void setBleServiceListener() { mBleService.setOnConnectListener(new MultipleBleService.OnConnectionStateChangeListener() { @Override/*ww w . j a v a2s.c om*/ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_DISCONNECTED) { for (int i = 0; i < deviceList.size(); i++) { HashMap<String, Object> devMap = (HashMap<String, Object>) deviceList.get(i); if (devMap.get("address").toString().equals(gatt.getDevice().getAddress())) { ((HashMap) deviceList.get(i)).put("isConnect", false); return; } } } else if (newState == BluetoothProfile.STATE_CONNECTING) { } else if (newState == BluetoothProfile.STATE_CONNECTED) { for (int i = 0; i < deviceList.size(); i++) { HashMap<String, Object> devMap = (HashMap<String, Object>) deviceList.get(i); if (devMap.get("address").toString().equals(gatt.getDevice().getAddress())) { ((HashMap) deviceList.get(i)).put("isConnect", true); return; } } } else if (newState == BluetoothProfile.STATE_DISCONNECTING) { } } }); mBleService.setOnDataAvailableListener(new MultipleBleService.OnDataAvailableListener() { @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { } @Override public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { } }); }
From source file:com.beestar.ble.ble.ui.MultipleBleActivity.java
private void setBleServiceListener() { mBleService.setOnConnectListener(new MultipleBleService.OnConnectionStateChangeListener() { @Override/*from w w w . j ava 2s.c o m*/ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_DISCONNECTED) { for (int i = 0; i < deviceList.size(); i++) { HashMap<String, Object> devMap = (HashMap<String, Object>) deviceList.get(i); if (devMap.get("address").toString().equals(gatt.getDevice().getAddress())) { ((HashMap) deviceList.get(i)).put("isConnect", false); return; } } } else if (newState == BluetoothProfile.STATE_CONNECTING) { } else if (newState == BluetoothProfile.STATE_CONNECTED) { for (int i = 0; i < deviceList.size(); i++) { HashMap<String, Object> devMap = (HashMap<String, Object>) deviceList.get(i); if (devMap.get("address").toString().equals(gatt.getDevice().getAddress())) { ((HashMap) deviceList.get(i)).put("isConnect", true); return; } } } else if (newState == BluetoothProfile.STATE_DISCONNECTING) { } } }); //?? mBleService.setOnServicesDiscoveredListener(new MultipleBleService.OnServicesDiscoveredListener() { @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { String characteristics_uuid_read = null; String characteristics_uuid_write = null; String read_Uuid = null; String write_Uuid = null; BluetoothGattCharacteristic characteristic; if (status == gatt.GATT_SUCCESS) { String address = gatt.getDevice().getAddress(); List<BluetoothGattService> services = gatt.getServices(); commonUtils = new CommonUtils(getApplicationContext()); int j, k; for (int i = 0; i < services.size(); i++) { if (services.get(i).getUuid().toString().contains("ffe5")) { write_Uuid = services.get(i).getUuid().toString(); Log.i("info", "--------------------" + i); characteristics_uuid_write = services.get(i).getCharacteristics().get(0).getUuid() .toString(); } if (services.get(i).getUuid().toString().contains("ffe0")) { Log.i("info", "--------------------" + i); characteristic = services.get(i).getCharacteristic(services.get(i).getUuid()); read_Uuid = services.get(i).getUuid().toString(); characteristics_uuid_read = services.get(i).getCharacteristics().get(0).getUuid() .toString(); } } Equipment_Device equipment_device = new Equipment_Device(); equipment_device.setAddress(address); equipment_device.setRead_uuid(read_Uuid); equipment_device.setRead_uuid(write_Uuid); equipment_device.setCh_read_uuid(characteristics_uuid_read); equipment_device.setCh_write_uuid(characteristics_uuid_write); commonUtils.insertStudent(equipment_device); } } }); mBleService.setOnConnectListener(new MultipleBleService.OnConnectionStateChangeListener() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { } }); mBleService.setOnDataAvailableListener(new MultipleBleService.OnDataAvailableListener() { @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { } @Override public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { } }); }
From source file:com.google.android.apps.forscience.ble.MyBleService.java
public void disconnectDevice(String address) { BluetoothGatt bluetoothGatt = addressToGattClient.get(address); if (btAdapter == null || address == null || bluetoothGatt == null) { return;/*from www . jav a 2 s. c o m*/ } BluetoothDevice device = btAdapter.getRemoteDevice(address); int bleState = bluetoothManager.getConnectionState(device, BluetoothProfile.GATT); if (bleState != BluetoothProfile.STATE_DISCONNECTED && bleState != BluetoothProfile.STATE_DISCONNECTING) { bluetoothGatt.disconnect(); } else { bluetoothGatt.close(); addressToGattClient.remove(address); sendGattBroadcast(address, BleEvents.GATT_DISCONNECT, null); } }
From source file:com.mooshim.mooshimeter.main.ScanActivity.java
public synchronized void startScan() { if (mScanOngoing) return;// w w w .j ava 2 s . c o m mScanOngoing = true; final Handler h = new Handler(); mBtnScan.setEnabled(false); updateScanningButton(false); // Prune disconnected meters List<MooshimeterDevice> remove = new ArrayList<MooshimeterDevice>(); for (MooshimeterDevice m : mMeterList) { if (m.mConnectionState == BluetoothProfile.STATE_DISCONNECTED) { remove.add(m); } } for (MooshimeterDevice m : remove) { mDeviceScrollView.removeView(mTileList.remove(mMeterList.indexOf(m))); mMeterList.remove(m); mMeterDict.remove(m.getAddress()); } refreshAllMeterTiles(); final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (!bluetoothAdapter.startLeScan(mLeScanCallback)) { // Starting the scan failed! Log.e(TAG, "Failed to start BLE Scan"); setError("Failed to start scan"); } h.postDelayed(new Runnable() { @Override public void run() { mBtnScan.setEnabled(true); updateScanningButton(false); final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); bluetoothAdapter.stopLeScan(mLeScanCallback); mScanOngoing = false; } }, 5000); }