本文整理匯總了Java中android.bluetooth.BluetoothGatt.STATE_DISCONNECTED屬性的典型用法代碼示例。如果您正苦於以下問題:Java BluetoothGatt.STATE_DISCONNECTED屬性的具體用法?Java BluetoothGatt.STATE_DISCONNECTED怎麽用?Java BluetoothGatt.STATE_DISCONNECTED使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類android.bluetooth.BluetoothGatt
的用法示例。
在下文中一共展示了BluetoothGatt.STATE_DISCONNECTED屬性的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: disconnect
@Override
public void disconnect() {
NeatleLogger.i("Disconnecting");
stopDiscovery();
BluetoothGatt target;
int oldState;
synchronized (lock) {
target = gatt;
gatt = null;
this.serviceDiscovered = false;
oldState = state;
state = BluetoothGatt.STATE_DISCONNECTED;
}
if (target != null) {
target.disconnect();
}
notifyConnectionStateChange(oldState, BluetoothGatt.STATE_DISCONNECTED);
}
示例2: connectWithGatt
private void connectWithGatt() {
int oldState;
int newState = BluetoothGatt.STATE_CONNECTING;
synchronized (lock) {
oldState = state;
state = BluetoothGatt.STATE_CONNECTING;
}
NeatleLogger.d("Connecting with " + device.getName() + "[" + device.getAddress() + "]");
BluetoothGatt gatt = device.connectGatt(context, false, callback);
synchronized (lock) {
this.gatt = gatt;
if (gatt == null) {
state = BluetoothGatt.STATE_DISCONNECTED;
newState = BluetoothGatt.STATE_DISCONNECTED;
}
}
notifyConnectionStateChange(oldState, newState);
}
示例3: onConnectionStateChange
/**
* 連接狀態改變回調
* 係統自帶API
* @param gatt
* @param status
* @param newState
*/
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (BleLog.isPrint) {
BleLog.i(TAG, "onConnectionStateChange status: " + status
+ " ,newState: " + newState + " ,thread: " + Thread.currentThread().getId());
}
if (newState == BluetoothGatt.STATE_CONNECTED) {
connectionState = STATE_CONNECTED;
// 連接成功
onConnectSuccess(gatt, status);
} else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
connectionState = STATE_DISCONNECTED;
// 連接失敗
onConnectFailure(new ConnectException(gatt, status));
} else if (newState == BluetoothGatt.STATE_CONNECTING) {
connectionState = STATE_CONNECTING;
}
// 遍曆回調回所有傳進了的回調, 給子類處理
for (BluetoothGattCallback call : callbackList) {
call.onConnectionStateChange(gatt, status, newState);
}
}
示例4: onConnectionStateChange
/**
* Connection State callback. This is called any time a device connects/disconnects, etc. We
* only handle a single device at a time, and ignore any other connection requests. When a
* connection is established, we immediately send the APDUInterface (i.e., this object) to the
* waiting Activity.
*/
@Override
public synchronized void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
Log.v(TAG, "onConnectionStateChange: " + deviceString(device) + ", " + status + ", " + newState);
if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothGatt.STATE_CONNECTED) {
/* new device */
if (currentDevice == null) {
currentDevice = device;
Log.v(TAG, "Connected to device: " + deviceString(device));
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.loadLicense(GattService.this);
}
} );
} else {
Log.v(TAG, "Already connected to: " + deviceString(currentDevice) + ", ignoring new device " + deviceString(device));
}
} else if (status != BluetoothGatt.GATT_SUCCESS || newState == BluetoothGatt.STATE_DISCONNECTED){
if (!currentDevice.equals(device)) {
Log.v(TAG, "Ignoring state change of device " + deviceString(device));
} else {
Log.v(TAG, "Disconnecting from " + deviceString(device));
currentDevice = null;
notifyAll();
}
}
}
示例5: connectionFailed
private void connectionFailed(int status) {
BluetoothGattCallback current;
int oldState;
int newState;
LinkedList<BluetoothGattCallback> queueCopy;
synchronized (lock) {
oldState = state;
state = BluetoothGatt.STATE_DISCONNECTED;
newState = state;
serviceDiscovered = false;
current = currentCallback;
queueCopy = new LinkedList<>(queue);
}
NeatleLogger.i("Connection attempt failed. Notifying all pending operations");
current.onConnectionStateChange(this.gatt, status, BluetoothGatt.STATE_DISCONNECTED);
for (BluetoothGattCallback cb : queueCopy) {
cb.onConnectionStateChange(gatt, status, BluetoothGatt.STATE_DISCONNECTED);
}
synchronized (lock) {
this.gatt = null;
}
notifyConnectionStateChange(oldState, newState);
}
示例6: onConnectionStateChange
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (status == BluetoothGatt.GATT_SUCCESS) {
switch (newState) {
case BluetoothGatt.STATE_CONNECTED:
bleHandler.obtainMessage(MSG_CONNECTED, gatt).sendToTarget();
break;
case BluetoothGatt.STATE_DISCONNECTED:
bleHandler.obtainMessage(MSG_DISCONNECTED, gatt).sendToTarget();
break;
}
}
}
示例7: onConnectionStateChange
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.e(LOG_TAG, "onConnectionStateChange from " + status + " to "+ newState + " on peripheral:" + device.getAddress()+" gatt:" + gatt);
this.gatt = gatt;
if (newState == BluetoothGatt.STATE_CONNECTED) {
connected = true;
trytime=0;
// try {
// Thread.sleep(500);
// }catch (Exception e){}
gatt.discoverServices();
sendConnectionEvent(device, "BleManagerConnectPeripheral");
} else if (newState == BluetoothGatt.STATE_DISCONNECTED){
Log.e(LOG_TAG,"connect="+connected+" gatt="+gatt);
if (connected) {
connected = false;
}
if (this.gatt != null) {
this.gatt.close();
this.gatt = null;
}
if(status == 133)
{
if (this.gatt != null) {
this.gatt.close();
this.gatt = null;
}
trytime++;
Log.e(LOG_TAG, "trytime="+trytime);
if(trytime < 2)
{
connect(connectCallback,mActivity);
return;
}
}
trytime=0;
sendConnectionEvent(device, "BleManagerDisconnectPeripheral");
if (connectCallback != null) {
connectCallback.invoke("Connection error");
connectCallback = null;
}
}
}
示例8: onConnectionStateChange
@Override
public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) {
// Check whether an error occurred
logi("onConnectionStateChange() :: Start");
if (status == BluetoothGatt.GATT_SUCCESS) {
if (newState == BluetoothGatt.STATE_CONNECTED) {
logi("onConnectionStateChange() :: Connected to GATT server");
mConnectionState = STATE_CONNECTED;
/*
* The onConnectionStateChange callback is called just after establishing connection and before sending Encryption Request BLE event in case of a paired device.
* In that case and when the Service Changed CCCD is enabled we will get the indication after initializing the encryption, about 1600 milliseconds later.
* If we discover services right after connecting, the onServicesDiscovered callback will be called immediately, before receiving the indication and the following
* service discovery and we may end up with old, application's services instead.
*
* This is to support the buttonless switch from application to bootloader mode where the DFU bootloader notifies the master about service change.
* Tested on Nexus 4 (Android 4.4.4 and 5), Nexus 5 (Android 5), Samsung Note 2 (Android 4.4.2). The time after connection to end of service discovery is about 1.6s
* on Samsung Note 2.
*
* NOTE: We are doing this to avoid the hack with calling the hidden gatt.refresh() method, at least for bonded devices.
*/
if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_BONDED) {
try {
synchronized (this) {
logd("onConnectionStateChange() :: Waiting 1600 ms for a possible Service Changed indication...");
wait(1600);
// After 1.6s the services are already discovered so the following gatt.discoverServices() finishes almost immediately.
// NOTE: This also works with shorted waiting time. The gatt.discoverServices() must be called after the indication is received which is
// about 600ms after establishing connection. Values 600 - 1600ms should be OK.
}
} catch (InterruptedException e) {
Log.e(TAG, e.toString());
// Do nothing
}
}
// Attempts to discover services after successful connection.
final boolean success = gatt.discoverServices();
logi("onConnectionStateChange() :: Attempting to start service discovery... " + (success ? "succeed" : "failed"));
if (!success) {
mError = ERROR_SERVICE_DISCOVERY_NOT_STARTED;
} else {
// Just return here, lock will be notified when service discovery finishes
return;
}
} else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
logi("onConnectionStateChange() :: Disconnected from GATT server");
mPaused = false;
mConnectionState = STATE_DISCONNECTED;
}
} else {
loge("Connection state change error: " + status + " newState: " + newState);
/* if (newState == BluetoothGatt.STATE_DISCONNECTED) {
mConnectionState = STATE_DISCONNECTED;
if (mServicePhase == PAIRING_REQUEST ){
mServicePhase = PAIRING_FAILED ;
updateProgressNotification(status);
}
}*/
mPaused = false;
mError = ERROR_CONNECTION_STATE_MASK | status;
}
// Notify waiting thread
synchronized (mLock) {
mLock.notifyAll();
}
}