最近也在做这个东西就分享给你吧,如果你的程序是在连接上之后启动的,你最好使用如下方式获取已连接的蓝牙地址:BluetoothAdapter _blueAdapter = BluetoothAdapter.getDefaultAdapter();
//注册a2dp监听
_blueAdapter.getProfileProxy(_mParent,proxyListener,BluetoothProfile.A2DP);private BluetoothProfile.ServiceListener proxyListener = new BluetoothProfile.ServiceListener(){ @Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
// TODO Auto-generated method stub
SLog.Log('w',TAG,"BluetoothProfile profile: " + profile);
if (proxy != null){
List<BluetoothDevice> cd = proxy.getConnectedDevices();
if (cd != null){
SLog.Log('w',TAG,"BluetoothDevice size: " + cd.size());
for (int i = 0 ; i < cd.size(); i++){
BluetoothDevice t = cd.get(i);
SLog.Log('w',TAG,"BluetoothDevice : " + t.getName() + "|" + t.getAddress()
);
if (reConnectDevice(t.getAddress())){
SLog.Log('w',TAG,"select BluetoothDevice : " + t.getName() +
"|" + t.getAddress() + " connect sucess");
break;
}
}
}
}
} @Override
public void onServiceDisconnected(int profile) {
// TODO Auto-generated method stub
}
};如果是蓝牙A2dp后连接的你最好接收下BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED广播如下处理:
else if (BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED.equalsIgnoreCase(action)){
BluetoothDevice mdevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int istate = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, -1);
int newState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0);
SLog.Log('w',TAG, "BluetoothA2dp ACTION_CONNECTION_STATE_CHANGED :" + istate + "|" + newState + "," + mdevice);
if (mdevice != null){
checkBlueToothA2dpState(mdevice,istate);
}
}
//注册a2dp监听
_blueAdapter.getProfileProxy(_mParent,proxyListener,BluetoothProfile.A2DP);private BluetoothProfile.ServiceListener proxyListener = new BluetoothProfile.ServiceListener(){ @Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
// TODO Auto-generated method stub
SLog.Log('w',TAG,"BluetoothProfile profile: " + profile);
if (proxy != null){
List<BluetoothDevice> cd = proxy.getConnectedDevices();
if (cd != null){
SLog.Log('w',TAG,"BluetoothDevice size: " + cd.size());
for (int i = 0 ; i < cd.size(); i++){
BluetoothDevice t = cd.get(i);
SLog.Log('w',TAG,"BluetoothDevice : " + t.getName() + "|" + t.getAddress()
);
if (reConnectDevice(t.getAddress())){
SLog.Log('w',TAG,"select BluetoothDevice : " + t.getName() +
"|" + t.getAddress() + " connect sucess");
break;
}
}
}
}
} @Override
public void onServiceDisconnected(int profile) {
// TODO Auto-generated method stub
}
};如果是蓝牙A2dp后连接的你最好接收下BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED广播如下处理:
else if (BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED.equalsIgnoreCase(action)){
BluetoothDevice mdevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int istate = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, -1);
int newState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0);
SLog.Log('w',TAG, "BluetoothA2dp ACTION_CONNECTION_STATE_CHANGED :" + istate + "|" + newState + "," + mdevice);
if (mdevice != null){
checkBlueToothA2dpState(mdevice,istate);
}
}
解决方案 »
- TabHost中的子Activity跳转到其它Activity的生命周期反常(可能和SurfaceView有关)
- ubuntu下编译啊android源码报错,大神指点,在线等
- android反编译之后代码还原
- 请问怎样判断程序运行在模拟器还是真机上?
- 新浪微博客户端---授权认证出错啦,求大家帮帮忙
- 通过基站CID,LAC等信息获取基站经纬度
- 怎样把android4.4的版本打包成2.3的版本
- 急 android拍照上传空指针
- android studio 上手使用 大水逼问题
- Android 连接了一个C#编写的socket服务端,该服务端部署到了云服务器上,为什么这个socket服务端关闭了,socket客户端依然提示连接成功
- android运行直接终止
- 运行helloworld程序的时候,提示 “Emulator PANIC: Could not open: AVD”
没反应,onServiceConnected函数没有执行啦!楼主搞定了吗?