在开发接收蓝牙4模块数据时,一直收不到模块的数据。但模块提供商写了demo是可以接收的。当用debug时运行到accept()时之后就像死了一样,下面的就执行不了。UUID定义如下:
private static final UUID MY_UUID=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
程序如下:
private class AcceptThread extends Thread{
public AcceptThread(){
BluetoothServerSocket tmp1 = null,tmp2=null;
try {
tmp1=mBluetoothAdapter.listenUsingRfcommWithServiceRecord("SERVICE1", MY_UUID);
tmp2=mBluetoothAdapter.listenUsingRfcommWithServiceRecord("SERVICE2", MY_UUID);
} catch (IOException e){
// TODO Auto-generated catch block
Log.e("MY", "Acceptthread init is error", e);
}
mBluetoothServerSocket=tmp1;
mBluetoothServerSocket1=tmp2;
}
public void run(){
try {
socket[0]=mBluetoothServerSocket.accept();
synchronized (this){
connectThread[0]=new ConnectThread(socket[0],socket[0].getRemoteDevice(),1);
connectThread[0].start();
}
mHandler.obtainMessage(9, -1, -1,null).sendToTarget();
mBluetoothServerSocket.close();
mBluetoothServerSocket=null;
socket[1]=mBluetoothServerSocket1.accept();
synchronized (this){
connectThread[1]=new ConnectThread(socket[1],socket[1].getRemoteDevice(),2);
connectThread[1].start();
}
mHandler.obtainMessage(10, -1, -1, null).sendToTarget();
mBluetoothServerSocket1.close();
mBluetoothServerSocket1=null;
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("TAG", "ACCEPT IS ERROR", e);
}
}
public void cancle(){
try {
if(mBluetoothServerSocket!=null){
mBluetoothServerSocket.close();
mBluetoothServerSocket=null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("MY", "mBluetoothServerSocket CLOSE IS ERROR!");
}
try {
if(mBluetoothServerSocket1!=null){
mBluetoothServerSocket1.close();
mBluetoothServerSocket1=null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("MY", "mBluetoothServerSocket1 CLOSE IS ERROR!");
}
}
}
private static final UUID MY_UUID=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
程序如下:
private class AcceptThread extends Thread{
public AcceptThread(){
BluetoothServerSocket tmp1 = null,tmp2=null;
try {
tmp1=mBluetoothAdapter.listenUsingRfcommWithServiceRecord("SERVICE1", MY_UUID);
tmp2=mBluetoothAdapter.listenUsingRfcommWithServiceRecord("SERVICE2", MY_UUID);
} catch (IOException e){
// TODO Auto-generated catch block
Log.e("MY", "Acceptthread init is error", e);
}
mBluetoothServerSocket=tmp1;
mBluetoothServerSocket1=tmp2;
}
public void run(){
try {
socket[0]=mBluetoothServerSocket.accept();
synchronized (this){
connectThread[0]=new ConnectThread(socket[0],socket[0].getRemoteDevice(),1);
connectThread[0].start();
}
mHandler.obtainMessage(9, -1, -1,null).sendToTarget();
mBluetoothServerSocket.close();
mBluetoothServerSocket=null;
socket[1]=mBluetoothServerSocket1.accept();
synchronized (this){
connectThread[1]=new ConnectThread(socket[1],socket[1].getRemoteDevice(),2);
connectThread[1].start();
}
mHandler.obtainMessage(10, -1, -1, null).sendToTarget();
mBluetoothServerSocket1.close();
mBluetoothServerSocket1=null;
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("TAG", "ACCEPT IS ERROR", e);
}
}
public void cancle(){
try {
if(mBluetoothServerSocket!=null){
mBluetoothServerSocket.close();
mBluetoothServerSocket=null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("MY", "mBluetoothServerSocket CLOSE IS ERROR!");
}
try {
if(mBluetoothServerSocket1!=null){
mBluetoothServerSocket1.close();
mBluetoothServerSocket1=null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("MY", "mBluetoothServerSocket1 CLOSE IS ERROR!");
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货