android 蓝牙模块连接


假设你已经做好android蓝牙模块的准备工作,既已经搜索到附近其他的蓝牙设备,在尝试进行连接时会出现失败,报错。在这分享下我的心得

首先我使用了两种连接方式,我发现这两种方式分别适合于不同场景,不能交换使用场景

方式一

try {
            Method m = device.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
            try {
                socket = (BluetoothSocket) m.invoke(device, 1);
                try {
                    socket.connect();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
通过反射机制拿到socket进行请求配对,这种方式适合于两个手机直接进行连接。

当我们通过代码创建蓝牙服务端时,上述方式并不能连接成功,就要用到

方式二:

 socket = device.createRfcommSocketToServiceRecord(UUID.fromString("服务端的UUID"));

当使用蓝牙模块外设时,我还没有测试,刚买它还在路上,根据上述描述,简单尝试即可。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Android蓝牙模块是指在Android设备上用于支持蓝牙功能的硬件和软件组件。通过蓝牙模块Android设备可以与其他蓝牙设备进行通信和交互。 以下是关于Android蓝牙模块的介绍和演示: 1. 获取BluetoothAdapter对象: ```java BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); ``` 这个方法返回一个BluetoothAdapter对象,用于管理设备的蓝牙功能。 2. 检查设备是否支持蓝牙: ```java if (mBluetoothAdapter == null) { showToast("没有发现蓝牙模块"); return; } ``` 通过判断BluetoothAdapter对象是否为null,可以确定设备是否支持蓝牙功能。 3. 打开或关闭蓝牙: ```java if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } ``` 可以使用isEnabled()方法检查蓝牙是否已经打开。如果蓝牙未打开,可以通过ACTION_REQUEST_ENABLE意图请求用户打开蓝牙。 4. 搜索蓝牙设备: ```java mBluetoothAdapter.startDiscovery(); ``` 使用startDiscovery()方法开始搜索附近的蓝牙设备。 5. 连接蓝牙设备: ```java BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid); socket.connect(); ``` 可以使用getRemoteDevice()方法获取要连接蓝牙设备对象,然后使用createRfcommSocketToServiceRecord()方法创建一个BluetoothSocket对象,并使用connect()方法连接到设备。 6. 发送和接收数据: ```java OutputStream outputStream = socket.getOutputStream(); outputStream.write(data); InputStream inputStream = socket.getInputStream(); byte[] buffer = new byte[1024]; int bytesRead = inputStream.read(buffer); ``` 可以使用getOutputStream()方法获取输出流,然后使用write()方法发送数据。使用getInputStream()方法获取输入流,然后使用read()方法接收数据。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风晴03

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值