android 实现蓝牙自动配对连接

BluetoothConnectActivityReceiver.java:监听蓝牙配对的广播

代码:

package com.imte.Broadcast;

import com.imte.utils.ClsUtils;
import com.itme.ActivityClass.R;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

public class BluetoothConnectActivityReceiver extends BroadcastReceiver {

 String strPsw = "123456";

 @Override
 public void onReceive(Context context, Intent intent) {
  // TODO Auto-generated method stub
  if (intent.getAction().equals(
    "android.bluetooth.device.action.PAIRING_REQUEST")) {
   BluetoothDevice btDevice = intent
     .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
   try {
    ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw); // 手机和蓝牙采集器配对
    ClsUtils.createBond(btDevice.getClass(), btDevice);
    ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice);
    Toast.makeText(
      context,
      context.getResources().getString(
        R.string.bluetooth_connect_success),
      Toast.LENGTH_SHORT);
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
//    Thread thread=new Thread(strPsw);
//    thread.
   }
  }

 }
}

ClsUtils.java:里面主要是一些通过反射机制得到蓝牙配对的相关方法,代码如下:

package com.imte.utils;

/************************************ 蓝牙配对函数 * *************

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Android操作系统具有本身具有蓝牙功能,并且提供蓝牙API,方便开发人员进行蓝牙开发。本文将介绍如何创建一个蓝牙搜索、自动配对和通信的Demo。 首先,在应用程序清单文件中请求蓝牙权限。在应用程序清单文件中,应该添加以下代码: ``` <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> ``` 然后,在MainActivity中,创建一个BluetoothAdapter的实例并启用蓝牙。接下来,创建一个广播接收器来响应搜索、配对连接的事件。使用设备的名称和地址作为数据存储在搜索结果中。然后,在UI上显示搜索到的设备列表。在使用设备进行通信之前,需要与其进行配对。使用createBond()方法自动配对设备。 ``` bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (!bluetoothAdapter.isEnabled()) { Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent, REQUEST_ENABLE_BT); } final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); String name = device.getName(); String address = device.getAddress(); // add device to list deviceList.add(name + "\n" + address); listAdapter.notifyDataSetChanged(); } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() == BluetoothDevice.BOND_BONDED) { // connect to device for communication connectToDevice(device); } } } }; searchButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { deviceList.clear(); bluetoothAdapter.startDiscovery(); registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED)); } }); private void connectToDevice(BluetoothDevice device) { BluetoothSocket socket = null; try { socket = device.createRfcommSocketToServiceRecord(MY_UUID); socket.connect(); // start communication } catch (IOException e) { e.printStackTrace(); } } ``` 在这个Demo中,当用户点击搜索按钮时,应用程序将开始搜索周围的设备。当找到设备时,列表将显示设备的名称和地址。然后,当用户选择列表中的设备时,设备将尝试自动配对。如果配对成功,应用程序将使用BluetoothSocket进行通信。 总之,这是一个简单但完整的Android蓝牙搜索、自动配对和通信的Demo,用于学习蓝牙开发的基本思想和方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值