Java 类android.bluetooth.BluetoothProfile 实例源码
项目:lrs_android
文件:AgoraActivity.java
private void optional() {
HeadsetPlugManager.getInstance().registerHeadsetPlugListener(this);
mHeadsetListener = new HeadsetBroadcastReceiver();
registerReceiver(mHeadsetListener, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
mBluetoothHeadsetBroadcastListener = new BluetoothHeadsetBroadcastReceiver();
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBtAdapter != null && BluetoothProfile.STATE_CONNECTED == mBtAdapter.getProfileConnectionState(BluetoothProfile.HEADSET)) {
// on some devices, BT is not supported
boolean bt = mBtAdapter.getProfileProxy(getBaseContext(), mBluetoothHeadsetListener, BluetoothProfile.HEADSET);
int connection = mBtAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
}
IntentFilter i = new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
i.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
i.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
registerReceiver(mBluetoothHeadsetBroadcastListener, i);
//避免对window添加ui修改参数
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
}
项目:Make-A-Pede-Android-App
文件:BluetoothLeService.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_CONNECTED;
connectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
Log.i(TAG, "Attempting to start service discovery:" + bluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_DISCONNECTED;
connectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
项目:Linphone4Android
文件:BluetoothManager.java
public void stopBluetooth() {
Log.w("[Bluetooth] Stopping...");
isBluetoothConnected = false;
disableBluetoothSCO();
if (mBluetoothAdapter != null && mProfileListener != null && mBluetoothHeadset != null) {
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
mProfileListener = null;
}
mBluetoothDevice = null;
Log.w("[Bluetooth] Stopped!");
if (LinphoneManager.isInstanciated()) {
LinphoneManager.getInstance().routeAudioToReceiver();
}
}
项目:TrainAppTFG
文件:BluetoothLeService.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
Log.d("BLUETOOTH", "Estado de conexión bluetooth: " + (newState == BluetoothProfile.STATE_CONNECTED ? "Connected" : "Disconnected"));
if(newState == BluetoothProfile.STATE_CONNECTED){
//setState(State.CONNECTED);
mBluetoothGatt.discoverServices();
}
else{
//setState(State.IDDLE);
//TODO Realizar todas las tareas necesarias cuando se desconecte la pulsera
Log.d("BLUETOOTH", "Se ha desconectado el dispostivo bluetooth");
}
}
项目:arduator
文件:ArduinoCommBle.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {
connected = true;
gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
connected = false;
btGatt.close();
btGatt = null;
charSerial = null;
messenger.obtainMessage(MessageConstants.STATUS, StatusCode.Disconnected).sendToTarget();
} else {
messenger.obtainMessage(MessageConstants.ERROR, ErrorCode.Connect).sendToTarget();
taskConnectTimeout.cancel();
}
}
项目:nc-android-webrtcpeer
文件:BluetoothManager.java
/**
* Stops and closes all components related to Bluetooth audio.
*/
public void stop() {
ThreadUtils.checkIsOnMainThread();
Log.d(TAG, "stop: BT state=" + bluetoothState);
if (bluetoothAdapter == null) {
return;
}
// Stop BT SCO connection with remote device if needed.
stopScoAudio();
// Close down remaining BT resources.
if (bluetoothState == State.UNINITIALIZED) {
return;
}
unregisterReceiver(bluetoothHeadsetReceiver);
cancelTimer();
if (bluetoothHeadset != null) {
bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset);
bluetoothHeadset = null;
}
bluetoothAdapter = null;
bluetoothDevice = null;
bluetoothState = State.UNINITIALIZED;
Log.d(TAG, "stop done: BT state=" + bluetoothState);
}
项目:lrs_android
文件:AgoraActivity.java
private void optionalDestroy() {
if (mBtAdapter != null) {
mBtAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothProfile);
mBluetoothProfile = null;
mBtAdapter = null;
}
if (mBluetoothHeadsetBroadcastListener != null) {
unregisterReceiver(mBluetoothHeadsetBroadcastListener);
mBluetoothHeadsetBroadcastListener = null;
}
if (mHeadsetListener != null) {
unregisterReceiver(mHeadsetListener);
mHeadsetListener = null;
}
HeadsetPlugManager.getInstance().unregisterHeadsetPlugListener(this);
}
项目:BLE-PEPS
文件:BluetoothLeClass.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
if(mOnConnectListener!=null)
mOnConnectListener.onConnect(gatt);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
if(mOnDisconnectListener!=null)
mOnDisconnectListener.onDisconnect(gatt);
Log.i(TAG, "Disconnected from GATT server.");
}
if(mOnConnectStatusChangedListener!=null)
mOnConnectStatusChangedListener.onConnectStatusChanged(gatt,status,newState);
Log.i(TAG, "Changed");
}
项目:BLE-PEPS
文件:BluetoothLeClass.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
if(mOnConnectListener!=null)
mOnConnectListener.onConnect(gatt);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
if(mOnDisconnectListener!=null)
mOnDisconnectListener.onDisconnect(gatt);
Log.i(TAG, "Disconnected from GATT server.");
}
if(mOnConnectStatusChangedListener!=null)
mOnConnectStatusChangedListener.onConnectStatusChanged(gatt,status,newState);
Log.i(TAG, "Changed");
}
项目:sdc-1-quickstart-android
文件:MainActivity.java
/**
* Callback indicating when GATT client has connected/disconnected to/from a remote
* GATT server.
*
* @param gatt GATT client
* @param status Status of the connect or disconnect operation.
* {@link BluetoothGatt#GATT_SUCCESS} if the operation succeeds.
* @param newState Returns the new connection state. Can be one of
* {@link android.bluetooth.BluetoothProfile#STATE_DISCONNECTED} or
* {@link android.bluetooth.BluetoothProfile#STATE_CONNECTED}
*/
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
// boolean indicating whether or not the next step is successful, default is false
boolean success = false;
// Start Service discovery if we're now connected
if (status == BluetoothGatt.GATT_SUCCESS) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
success = gatt.discoverServices();
} // else: not connected, continue
} // else: not successful
onStep(gatt, success);
}
项目:buildAPKsSamples
文件:BluetoothHDPService.java
/**
* Make sure Bluetooth and health profile are available on the Android device. Stop service
* if they are not available.
*/
@Override
public void onCreate() {
super.onCreate();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
// Bluetooth adapter isn't available. The client of the service is supposed to
// verify that it is available and activate before invoking this service.
stopSelf();
return;
}
if (!mBluetoothAdapter.getProfileProxy(this, mBluetoothServiceListener,
BluetoothProfile.HEALTH)) {
Toast.makeText(this, R.string.bluetooth_health_profile_not_available,
Toast.LENGTH_LONG);
stopSelf();
return;
}
}
项目:igrow-android
文件:BluetoothLeService.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
项目:AppRTC-Android
文件:AppRTCBluetoothManager.java
/** Stops and closes all components related to Bluetooth audio. */
public void stop() {
ThreadUtils.checkIsOnMainThread();
Log.d(TAG, "stop: BT state=" + bluetoothState);
if (bluetoothAdapter == null) {
return;
}
// Stop BT SCO connection with remote device if needed.
stopScoAudio();
// Close down remaining BT resources.
if (bluetoothState == State.UNINITIALIZED) {
return;
}
unregisterReceiver(bluetoothHeadsetReceiver);
cancelTimer();
if (bluetoothHeadset != null) {
bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset);
bluetoothHeadset = null;
}
bluetoothAdapter = null;
bluetoothDevice = null;
bluetoothState = State.UNINITIALIZED;
Log.d(TAG, "stop done: BT state=" + bluetoothState);
}
项目:AndroidRTC
文件:AppRTCBluetoothManager.java
/**
* Stops and closes all components related to Bluetooth audio.
*/
public void stop() {
ThreadUtils.checkIsOnMainThread();
unregisterReceiver(bluetoothHeadsetReceiver);
Log.d(TAG, "stop: BT state=" + bluetoothState);
if (bluetoothAdapter != null) {
// Stop BT SCO connection with remote device if needed.
stopScoAudio();
// Close down remaining BT resources.
if (bluetoothState != State.UNINITIALIZED) {
cancelTimer();
if (bluetoothHeadset != null) {
bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset);
bluetoothHeadset = null;
}
bluetoothAdapter = null;
bluetoothDevice = null;
bluetoothState = State.UNINITIALIZED;
}
}
Log.d(TAG, "stop done: BT state=" + bluetoothState);
}
项目:bluewatcher
文件:BluetoothClientService.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i(TAG, "Connected to GATT server.");
if( gatt.getDevice().getBondState() != BluetoothDevice.BOND_BONDED ) {
broadcastUpdate(ACTION_NOT_PAIRED);
gatt.disconnect();
return;
}
Log.i(TAG, "Attempting to start service discovery" );
mBluetoothGatt.discoverServices();
connected = true;
broadcastUpdate(ACTION_GATT_CLIENT_CONNECTED);
}
else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_CLIENT_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
connected = false;
}
}
项目:PairingExample
文件:MainActivity.java
public void run()
{
/* In example we don't start discovery, but any case if it is discovering, stop it */
if(failedConnect != true) {
/* Create profile listeners for A2DP and Headset profiles */
mBluetoothAdapter.getProfileProxy(getApplicationContext(), mProfileListener, BluetoothProfile.A2DP);
mBluetoothAdapter.getProfileProxy(getApplicationContext(), mProfileListener, BluetoothProfile.HEADSET);
/* Paired with Classic BT, update UI */
classic_paired = true;
tryBLE = true;
TextUpdateHandler.post(updateRunnable);
read_ble_handler.postDelayed(runnable, 5000);
}
}
项目:Android-BLE-to-Arduino
文件:BluetoothLeService.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
项目:Snach-Android
文件:BLEManager.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
resetConnectionTimeoutChecker();
isSnachConnected = true;
BLEManager.this.gatt = gatt; // TODO check if right device is discovered (instead of another BLE device with a different GATT..)
Log.i("BLE", "Connected to GATT server.");
Log.i("BLE", "Attempting to start service discovery:" + startServiceDiscovery(gatt));
sendBroadcastConnectionChanged(isSnachConnected);
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
isSnachConnected = false;
mConnectionListener.ConnectionLost();
Log.i("BLE", "Disconnected from GATT server.");
}
}
项目:UDOOBluLib-android
文件:UdooBluService.java
/**
* Connects to the GATT server hosted on the Bluetooth LE device.
*
* @param address The device address of the destination device.
* @return Return true if the connection is initiated successfully. The connection result is reported asynchronously through the
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} callback.
*/
public void connect(final String address, IBleDeviceListener iBleDeviceListener) {
UdooBluException udooBluException = checkBluetooth(getApplicationContext());
if (udooBluException != null) {
if (iBleDeviceListener != null)
iBleDeviceListener.onError(udooBluException);
} else {
final BluetoothDevice device = mBtAdapter.getRemoteDevice(address);
int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
BluetoothGatt bluetoothGatt = checkAndGetGattItem(address);
if (connectionState == BluetoothProfile.STATE_DISCONNECTED) {
// Previously connected device. Try to reconnect.
if (bluetoothGatt != null) {
Log.d(TAG, "Re-use GATT connection");
if (bluetoothGatt.connect()) {
} else {
Log.w(TAG, "GATT re-connect failed.");
}
} else if (device == null) {
Log.w(TAG, "Device not found. Unable to connect.");
} else {
Log.d(TAG, "Create a new GATT connection.");
bluetoothGatt = device.connectGatt(this, false, bluetoothGattCallbackBuilder());
mBluetoothGatts.put(address, bluetoothGatt);
}
} else {
Log.w(TAG, "Attempt to connect in state: " + connectionState);
bond(address);
bluetoothGatt = device.connectGatt(this, false, bluetoothGattCallbackBuilder());
mBluetoothGatts.put(address, bluetoothGatt);
}
}
}
项目:UDOOBluLib-android
文件:UdooBluService.java
/**
* Disconnects an existing connection or cancel a pending connection. The disconnection result is reported asynchronously through the
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} callback.
*/
public void disconnect(String address) {
if (mBtAdapter == null) {
Log.w(TAG, "disconnect: BluetoothAdapter not initialized");
return;
}
final BluetoothDevice device = mBtAdapter.getRemoteDevice(address);
int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
BluetoothGatt bluetoothGatt = checkAndGetGattItem(address);
if (bluetoothGatt != null) {
Log.i(TAG, "disconnect");
if (connectionState != BluetoothProfile.STATE_DISCONNECTED) {
bluetoothGatt.disconnect();
} else {
Log.w(TAG, "Attempt to disconnect in state: " + connectionState);
}
}
}
项目:MagicLight-Controller
文件:BluetoothLeService.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.e(TAG, "onConnectionStateChanged");
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) { // STATE : connected
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction); // broadcast Gatt connection state : connected
Log.e(TAG, "Connected to GATT server.");
Log.e(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // STATE : disconnected
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
broadcastUpdate(intentAction); // broadcast Gatt connection state : disconnected
Log.e(TAG, "Disconnected from GATT server.");
}
}
项目:mi-band-2
文件:BLEMiBand2Helper.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
{
switch(newState)
{
case BluetoothProfile.STATE_CONNECTED:
Log.d(TAG, "Gatt state: connected");
gatt.discoverServices();
isConnectedToGatt = true;
raiseonConnect();
break;
default:
Log.d(TAG, "Gatt state: not connected");
isConnectedToGatt = false;
raiseonDisconnect();
break;
}
}
项目:AndroidthingsStudy
文件:BluetoothHDPService.java
/**
* Make sure Bluetooth and health profile are available on the Android device. Stop service
* if they are not available.
*/
@Override
public void onCreate() {
super.onCreate();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
// Bluetooth adapter isn't available. The client of the service is supposed to
// verify that it is available and activate before invoking this service.
stopSelf();
return;
}
if (!mBluetoothAdapter.getProfileProxy(this, mBluetoothServiceListener,
BluetoothProfile.HEALTH)) {
Toast.makeText(this, R.string.bluetooth_health_profile_not_available,
Toast.LENGTH_LONG);
stopSelf();
return;
}
}
项目:sample-bluetooth-audio
文件:A2dpSinkActivity.java
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(A2dpSinkHelper.ACTION_CONNECTION_STATE_CHANGED)) {
int oldState = A2dpSinkHelper.getPreviousProfileState(intent);
int newState = A2dpSinkHelper.getCurrentProfileState(intent);
BluetoothDevice device = A2dpSinkHelper.getDevice(intent);
Log.d(TAG, "Bluetooth A2DP sink changing connection state from " + oldState +
" to " + newState + " device " + device);
if (device != null) {
String deviceName = Objects.toString(device.getName(), "a device");
if (newState == BluetoothProfile.STATE_CONNECTED) {
speak("Connected to " + deviceName);
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
speak("Disconnected from " + deviceName);
}
}
}
}
项目:Android-nRF-Beacon-for-Eddystone
文件:UpdateService.java
/**
* Disconnects from the device and closes the Bluetooth GATT object afterwards.
*/
public void disconnectAndClose() {
// This sometimes happen when called from UpdateService.ACTION_GATT_ERROR event receiver in UpdateFragment.
if (mBluetoothGatt == null)
return;
setState(STATE_DISCONNECTING);
mBluetoothGatt.disconnect();
// Sometimes the connection gets error 129 or 133. Calling disconnect() method does not really disconnect... sometimes the connection is already broken.
// Here we have a security check that notifies UI about disconnection even if onConnectionStateChange(...) has not been called.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (mConnectionState == STATE_DISCONNECTING)
mGattCallback.onConnectionStateChange(mBluetoothGatt, BluetoothGatt.GATT_SUCCESS, BluetoothProfile.STATE_DISCONNECTED);
}
}, 1500);
}
项目:Android-nRF-Beacon-for-Eddystone
文件:UpdateFragment.java
@Override
public void onDeviceSelected(final BluetoothDevice device, final String name) {
if (mConnectionProgressDialog != null) {
mConnectionProgressDialog.setTitle(getString(R.string.prog_dialog_connect_title));
mConnectionProgressDialog.setMessage(getString(R.string.prog_dialog_connect_message));
mConnectionProgressDialog.show();
}
final Activity activity = getActivity();
final Intent service = new Intent(activity, UpdateService.class);
service.putExtra(UpdateService.EXTRA_DATA, device);
updateUiForBeacons(BluetoothProfile.STATE_CONNECTED, UpdateService.LOCKED);
activity.startService(service);
mBounnd = true;
activity.bindService(service, mServiceConnection, 0);
}
项目:ssj
文件:BLESensorListener.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
}
}
项目:BLEServerSimple
文件:ANCSGattCallback.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
Log.i(TAG, "onConnectionStateChange,newState " + newState + "status:" + status);
try{
mBleState = BleStatus.values()[newState];
}catch(Exception e){
e.printStackTrace();
}
notifyListeners();
if (newState == BluetoothProfile.STATE_CONNECTED
&& status == BluetoothGatt.GATT_SUCCESS) {
// mBluetoothGatt = gatt;
mBleState = BleStatus.BUILD_DISCOVER_SERVICE;
notifyListeners();
gatt.discoverServices();
} else {
Log.i(TAG, "onConnectionStateChange,failure");
if(mOnGattDisconnectListener != null){
mOnGattDisconnectListener.onGattDisconnectListener(gatt);
}
}
}
项目:Grandroid2
文件:BluetoothLeService.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Config.logi("BluetoothGatt connection State Changed: [" + status + "]" + newState);
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction, gatt.getDevice().getAddress());
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to startScan findService discovery:" +
gatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
gatt.close();//make 133 not fire.
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction, gatt.getDevice().getAddress());
}
}
项目:android-xmpp-iot-demo
文件:XiotBluetoothLeManager.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
LOGGER.info("GATT connected: " + gatt);
gatt.discoverServices();
break;
case BluetoothProfile.STATE_DISCONNECTED:
LOGGER.info("GATT disconnected: " + gatt);
MainActivity.withMainActivity((ma) -> {
Toast.makeText(ma, "Connection to Polar H7 lost", Toast.LENGTH_SHORT).show();
});
mPolarH7BluetoothGatt = null;
resetHeartRateInformation();
break;
default:
throw new AssertionError();
}
}
项目:unreademailsformiband
文件:MiBandService.java
@Override
public void onConnectionStateChange(final BluetoothGatt gatt, int status, int newState)
{
super.onConnectionStateChange(gatt, status, newState);
Log.d(TAG, "onConnectionStateChange status="+status+" newState="+newState);
if (newState == BluetoothProfile.STATE_CONNECTED)
{
handler.post(new Runnable() {
@Override
public void run() {
gatt.discoverServices();
}
});
}
else if (newState == BluetoothProfile.STATE_DISCONNECTED)
{
//Util.sendBroadcast(getApplicationContext(), Constants.INTENT_MISCALE_DISCONNECTED);
closeGatt();
}
}
项目:AsteroidOSSync
文件:P_BleDevice_Listeners.java
private void onNativeConnectFail(final BluetoothGatt gatt, final int gattStatus)
{
//--- DRK > NOTE: Making an assumption that the underlying stack agrees that the connection state is STATE_DISCONNECTED.
//--- This is backed up by basic testing, but even if the underlying stack uses a different value, it can probably
//--- be assumed that it will eventually go to STATE_DISCONNECTED, so SweetBlue library logic is sounder "living under the lie" for a bit regardless.
m_device.m_nativeWrapper.updateNativeConnectionState(gatt, BluetoothProfile.STATE_DISCONNECTED);
final P_Task_Connect connectTask = m_queue.getCurrent(P_Task_Connect.class, m_device);
if (connectTask != null)
{
connectTask.onNativeFail(gattStatus);
}
else
{
m_device.onNativeConnectFail((PE_TaskState) null, gattStatus, AutoConnectUsage.UNKNOWN);
}
}
项目:AsteroidOSSync
文件:P_BleServer_Listeners.java
private void onNativeConnectFail(final BluetoothDevice nativeDevice, final int gattStatus)
{
//--- DRK > NOTE: Making an assumption that the underlying stack agrees that the connection state is STATE_DISCONNECTED.
//--- This is backed up by basic testing, but even if the underlying stack uses a different value, it can probably
//--- be assumed that it will eventually go to STATE_DISCONNECTED, so SweetBlue library logic is sounder "living under the lie" for a bit regardless.
m_server.m_nativeWrapper.updateNativeConnectionState(nativeDevice.getAddress(), BluetoothProfile.STATE_DISCONNECTED);
if( hasCurrentConnectTaskFor(nativeDevice) )
{
final P_Task_ConnectServer connectTask = m_queue.getCurrent(P_Task_ConnectServer.class, m_server);
connectTask.onNativeFail(gattStatus);
}
else
{
m_server.onNativeConnectFail(nativeDevice, BleServer.ConnectionFailListener.Status.NATIVE_CONNECTION_FAILED_EVENTUALLY, gattStatus);
}
}
项目:ai_ble_framework
文件:BluetoothLeService.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
项目:science-journal
文件:MyBleService.java
public void disconnectDevice(String address) {
BluetoothGatt bluetoothGatt = addressToGattClient.get(address);
if (btAdapter == null || address == null || bluetoothGatt == null) {
// Broadcast the disconnect so BleFlow doesn't hang waiting for it; something else
// already disconnected us in this case.
sendGattBroadcast(address, BleEvents.GATT_DISCONNECT, null);
return;
}
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);
}
}
项目:TappyBLE
文件:TappyBleDelegate.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if(gatt.getDevice().getAddress().equals(bleDeviceAddress)) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
changeState(TappyBleState.CONNECTED);
bluetoothGattRef.compareAndSet(null,gatt);
BluetoothGatt bluetoothGatt = bluetoothGattRef.get();
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
if (bluetoothGatt != null) {
Log.i(TAG, "Attempting to start service discovery:" +
bluetoothGatt.discoverServices());
} else {
Log.wtf(TAG, "Somehow connected with no gatt");
changeState(TappyBleState.ERROR);
}
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
changeState(TappyBleState.DISCONNECTED);
Log.i(TAG, "Disconnected from GATT server.");
}
}
}
项目:BLE_Tutorials
文件:Service_BTLE_GATT.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices());
}
else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
项目:mibandGeocaching
文件:BLECommunicationManager.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
{
mGatt = gatt;
switch(newState)
{
case BluetoothProfile.STATE_CONNECTED:
Log.d(TAG, "Gatt state: connected");
gatt.discoverServices();
mDeviceConnected = true;
if(mConnectionLatch != null)
{
mConnectionLatch.countDown();
}
break;
default:
Log.d(TAG, "Gatt state: not connected");
mDeviceConnected = false;
break;
}
}
项目:sample-android-ble
文件:BluetoothLeService.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
项目:xDrip
文件:ShareTest.java
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
mBluetoothGatt = gatt;
mConnectionState = STATE_CONNECTED;
ActiveBluetoothDevice.connected();
Log.i(TAG, "Connected to GATT server.");
Log.i(TAG, "Connection state: Bonded - " + device.getBondState());
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
currentGattTask = GATT_SETUP;
mBluetoothGatt.discoverServices();
} else {
device.setPin("000000".getBytes());
device.createBond();
}
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
mConnectionState = STATE_DISCONNECTED;
ActiveBluetoothDevice.disconnected();
Log.w(TAG, "Disconnected from GATT server.");
}
}