Created
June 11, 2016 03:50
-
-
Save liyinsg/2ca106bd3c35bc054d86e59184be5fc7 to your computer and use it in GitHub Desktop.
Bluetooth Tethering status monitor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<receiver android:name=".BtPanStateReceiver"> | |
<intent-filter> | |
<action android:name="android.bluetooth.pan.profile.action.CONNECTION_STATE_CHANGED" /> | |
</intent-filter> | |
</receiver> | |
<service android:name=".BtPanStateReceiver$BtPanService" android:exported="false"></service> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.st.modesetting; | |
import android.app.IntentService; | |
import android.app.Service; | |
import android.bluetooth.BluetoothAdapter; | |
import android.bluetooth.BluetoothDevice; | |
import android.bluetooth.BluetoothPan; | |
import android.bluetooth.BluetoothProfile; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.SystemClock; | |
import android.util.Log; | |
/** | |
* Created by ly on 11/6/16. | |
*/ | |
public class BtPanStateReceiver extends BroadcastReceiver { | |
public static class BtPanService extends IntentService { | |
private static final int CONNECT_PROXY_TIMEOUT = 5000; | |
private BluetoothAdapter mAdapter = null; | |
private BluetoothPan mPan = null; | |
private BluetoothProfile.ServiceListener mServiceListener = | |
new BluetoothProfile.ServiceListener() { | |
@Override | |
public void onServiceConnected(int profile, BluetoothProfile proxy) { | |
synchronized (this) { | |
if (profile == BluetoothProfile.PAN) | |
mPan = (BluetoothPan) proxy; | |
} | |
} | |
@Override | |
public void onServiceDisconnected(int i) { | |
mPan = null; | |
} | |
}; | |
public BtPanService() { | |
super("BtPanStateReceiver$BtPanService"); | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
Log.d("[LY]", "OnCreate"); | |
mAdapter = BluetoothAdapter.getDefaultAdapter(); | |
if (mAdapter == null) | |
return; | |
mAdapter.getProfileProxy(this, mServiceListener, BluetoothProfile.PAN); | |
} | |
@Override | |
public void onDestroy() { | |
Log.d("[LY]", "OnDestroy"); | |
mAdapter.closeProfileProxy(BluetoothProfile.PAN, mPan); | |
super.onDestroy(); | |
} | |
@Override | |
protected void onHandleIntent(Intent intent) { | |
Log.d("[LY]", "OnHandleIntent"); | |
long s = System.currentTimeMillis(); | |
while (mPan == null && System.currentTimeMillis() - s < CONNECT_PROXY_TIMEOUT) { | |
SystemClock.sleep(100); | |
} | |
int state = intent.getIntExtra( | |
BluetoothPan.EXTRA_STATE, BluetoothProfile.STATE_DISCONNECTED); | |
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); | |
if (!device.getName().startsWith("SIDEBRIDGE_")) { | |
Log.d("[LY]", "TODO: Gateway is not SIDEBRIDGE"); | |
stopService(intent); | |
return; | |
}; | |
switch (state) { | |
case BluetoothProfile.STATE_DISCONNECTED: | |
// Try to reconnect back | |
Log.d("[LY]", "Disconnected from " + device.getName() + ", try to connect back"); | |
int current_state = mPan.getConnectionState(device); | |
if (current_state == BluetoothPan.STATE_DISCONNECTED || | |
current_state == BluetoothPan.STATE_DISCONNECTING) | |
mPan.connect(device); | |
break; | |
case BluetoothProfile.STATE_CONNECTED: | |
// TODO: Connected, reset the failure count? | |
Log.d("[LY]", "Connected to " + device.getName()); | |
break; | |
} | |
stopService(intent); | |
} | |
} | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Intent i = new Intent(context, BtPanService.class); | |
i.putExtras(intent.getExtras()); | |
context.startService(i); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment