Android经典蓝牙开发uuid,关于android 经典蓝牙开发 使用UUID连接的问题

这篇博客探讨了在Android中遇到的蓝牙连接问题,作者最初使用固定UUID尝试连接,但遇到了读取失败的错误。通过查阅资料,作者采用了反射技术,创建了一个随机端口的蓝牙连接方法,从而成功解决了问题。博客详细介绍了反射原理在蓝牙连接中的应用,以及如何通过反射创建和管理蓝牙Socket。
摘要由CSDN通过智能技术生成

先贴一下我连接的代码,有点乱

开启蓝牙连接,由于连接是耗时的,所以肯定新开一个线程去连接,以下是连接的代码`package

/**

* Created by lyl on 2017/8/6.

*/

public class ConnectThread extends Thread {

private BluetoothDevice btd;

private BluetoothSocket bts;

private BluetoothController mController;

//00001101-0000-1000-8000-00805f9b34fb

public static final String MY_UUID = “00001101-0000-1000-8000-00805F9B34FB”;

private InputStream in;

private OutputStream out;

private static byte read_byts[] = new byte[120];

public static boolean isConnected = false;

public ConnectThread(BluetoothDevice btd) {

BluetoothSocket temp = null;

mController = new BluetoothController();

this.btd = btd;

try {

temp = btd.createInsecureRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));

} catch (IOException e) {

e.printStackTrace();

}

bts = temp;

}

private static android.os.Handler handler = new android.os.Handler() {

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

if (msg.what == 1) {

}

}

};

@Override

public void run() {

super.run();

//取消搜索因为搜索会让连接变慢

mController.getmAdapter().cancelDiscovery();

try {

//通过socket连接设备,这是一个阻塞操作,直到连接成功或发生异常

Log.e(TAG, "run: 连接1");

Log.e(TAG, "当前线程2:" + Thread.currentThread());

// bts.connect();

bts=connectBtByChannel(btd);

Log.e(TAG, "run: 连接2");

Log.e(TAG, "run: 连接成功" );

if (bts.isConnected()) {

isConnected = true;

in = bts.getInputStream();

out = bts.getOutputStream();

}

} catch (IOException e) {

Log.e(TAG, "run: 连接3——————————————————————》" + e.getMessage());

Log.e(TAG, "run: 连接3");

//无法连接,关闭socket并且退出

System.out.println("=========拒绝");

try {

bts.close();

} catch (IOException e1) {

e1.printStackTrace();

}

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

while (isConnected) {

try {

Log.e(TAG, "当前线程:" + Thread.currentThread());

int len = in.read(read_byts);

if (len != -1) {

Message me = Message.obtain();

me.what = 1;

handler.sendMessage(me);

}

} catch (IOException e) {

e.printStackTrace();

}

}

//管理连接(在独立的线程)

// manageConnectedSocket(mmSocket);

}

/* Call this from the main activity to send data to the remote device */

public void write(byte[] bytes) {

try {

out.write(bytes);

} catch (IOException e) {

}

}

public void cancel() {

try {

bts.close();

} catch (IOException e) {

e.printStackTrace();

}

}

private static int port = 0;

/**

* 通过反射原理,使用随机端口发起蓝牙连接

*/

public BluetoothSocket connectBtByChannel(BluetoothDevice mBluetoothDevice) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, IOException, InvocationTargetException {

Method method = mBluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});

port = port++ % 30;

if (port == 0) {

port = 1;

}

BluetoothSocket mBluetoothSocket = (BluetoothSocket) method.invoke(mBluetoothDevice, port);//1-30端口

mBluetoothSocket.connect();//发起连接

return mBluetoothSocket;

}

/**

* 通过反射原理,发起蓝牙连接

* */

public BluetoothSocket connectBtBySco(BluetoothDevice mBluetoothDevice) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, IOException{

Method method = mBluetoothDevice.getClass().getMethod("createScoSocket");

BluetoothSocket mBluetoothSocket = (BluetoothSocket)method.invoke(mBluetoothDevice);

mBluetoothSocket.connect();//发起连接

return mBluetoothSocket;

}

/**未配对过的,在连接建立之前先配对

* [url=home.php?mod=space&uid=2643633]@throws[/url] Exception */

public void createBond(BluetoothDevice mBluetoothDevice) throws Exception{

try {

// 连接建立之前的先配对

if (mBluetoothDevice.getBondState() == BluetoothDevice.BOND_NONE) {

Method creMethod = BluetoothDevice.class.getMethod("createBond");

Log.i("BluetoothManager", "开始配对");

creMethod.invoke(mBluetoothDevice);

}

} catch (Exception e) {

// TODO: handle exception

//DisplayMessage("无法配对!");

e.printStackTrace();

throw new Exception("Can't createBond with this device,please try again");

}

}

}

一开始我是使固定的UUID去获取一个BlueToothSocket

temp = btd.createInsecureRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));

拿到之后进行连接

bts.connect();

但是不知道什么原因一直返回

read failed, socket might closed or timeout, read ret: -1

可能是UUID的问题吧。

然后百度一下这个问题,也有很多人都碰到过这个问题,用了其中一个人的解决办法

地址是[http://www.eoeandroid.com/thread-913808-1-2.html?_dsign=16c362c1]得到了解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值