蓝牙是一种工作在全球通用的2.4GHz ISM(即工业、科学、医学)频段的短距离通信技术,通信距离在10m左右,其采用分散式网络结构以及快跳屏和短包技术,支持点对点及多点通信,已被广泛应用于智能手机及可穿戴设备领域等各个领域。本文主要介绍基于android手机的,简单的蓝牙连接,配对通信,供大家学习探讨!
蓝牙的打开搜索功能,主应用了动态广播,ArrayAdapter与ListView进行显示,回调函数onActivityResult主要用于确认蓝牙是否打开和为下一步通信做准备。
BluetoothAdapter代表本地蓝牙适配器,即通过此来获取本机蓝牙。
BluetoothDevice代表远程蓝牙设备,获取远程蓝牙信息。
android中蓝牙的打开、搜索主要分为以下几步。
1.在AndroidManifest.xml中添加蓝牙权限。
1
2 |
|
2.启动蓝牙
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
private boolean openBtDevice() {
// 获得蓝牙匹配器 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // 蓝牙设备不被支持 if (mBluetoothAdapter == null) { Log.e(TAG, "Your device is not support Bluetooth!"); Toast.makeText(this, "该设备没有蓝牙设备", Toast.LENGTH_LONG).show(); return false; } // 使能蓝牙设备 if (!mBluetoothAdapter.isEnabled()) { // 隐式Intent Intent enableBtIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUES_BT_ENABLE_CODE); } else { Toast.makeText(this, "蓝牙打开成功!", Toast.LENGTH_SHORT).show(); } return true; } // 当startActivityForResult启动的 画面结束的时候,该方法被回调 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUES_BT_ENABLE_CODE && resultCode == RESULT_OK) { Toast.makeText(this, "蓝牙打开成功!", Toast.LENGTH_SHORT).show(); } } |
3.搜索蓝牙
已经配对过的蓝牙设备
1
2 3 4 5 6 7 8 9 10 11 12 13 |
private void findBtDevice() {
Set if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { if (mDeviceList.contains(device)) { return; } mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); mDeviceList.add(device); } } adapter.notifyDataSetChanged(); } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(mReceiver, filter); //广播用于动态的添加搜索到的蓝牙到ListView中显示 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { //发现的蓝牙设备 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //发现的蓝牙设备已存在则跳过 if (mDeviceList.contains(device)) { return; } //添加发现的蓝牙设备 mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); mDeviceList.add(device); //更新到ListView adapter.notifyDataSetChanged(); } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { //搜索结束,关闭滚动条 setProgressBarIndeterminateVisibility(false); } } }; |
//下面是一个简单的蓝牙搜索显示蓝牙示例//
首先新建工程、接着在在AndroidManifest.xml中添加蓝牙权限。
1
2 |
|
编写布局文件
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
android:layout_height="match_parent" android:orientation="vertical" > android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="无内容" /> android:layout_height="wrap_content" android:orientation="horizontal" > android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:hint="请输入" /> |
/编写源代码//
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
public class MainActivity extends Activity {
private EditText mET; //发送内容,本示例未用到 private Button MyScan; //搜索按钮 private TextView MyMessage; //显示内容,本示例未用到 private ListView MyBtList; //显示蓝牙列表的ListView private BluetoothAdapter mBluetoothAdapter; //蓝牙适配器 private ArrayAdapter private ArrayList private ArrayList private static final String TAG = "MyBluetooth"; //调试用 private static final int REQUES_BT_ENABLE_CODE = 0x1002; // 蓝牙使能请求码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); //请求显示滚动条 setContentView(R.layout.activity_main); //UI注册 mET = (EditText) findViewById(R.id.SendText); MyScan = (Button) findViewById(R.id.FindBt); MyMessage = (TextView) findViewById(R.id.XianShi); MyBtList = (ListView) findViewById(R.id.DeviceList); //ListView添加适配器 adapter = new ArrayAdapter android.R.layout.simple_list_item_1, mArrayAdapter); MyBtList.setAdapter(adapter); openBtDevice(); //打开蓝牙 //搜索蓝牙按钮点击事件 MyScan.setOnClickListener(new MyScanClick()); IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); // 动态注册广播接收器 // 用来接收扫描到的设备信息 registerReceiver(mReceiver, filter); } //搜索蓝牙按钮点击事件 private class MyScanClick implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub if (!mBluetoothAdapter.isDiscovering()) { findBtDevice(); //打开蓝牙 mBluetoothAdapter.startDiscovery(); //开始搜索 setProgressBarIndeterminateVisibility(true); //显示滚动条 } } } //广播用于动态的添加搜索到的蓝牙到ListView中显示 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { //发现的蓝牙设备 BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //发现的蓝牙设备已存在则跳过 if (mDeviceList.contains(device)) { return; } //添加发现的蓝牙设备 mArrayAdapter .add(device.getName() + "\n" + device.getAddress()); mDeviceList.add(device); //更新到ListView adapter.notifyDataSetChanged(); } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED .equals(action)) { //搜索结束,关闭滚动条 setProgressBarIndeterminateVisibility(false); } } }; //之前已经配对的蓝牙,添加到ListView private void findBtDevice() { Set .getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { if (mDeviceList.contains(device)) { return; } mArrayAdapter .add(device.getName() + "\n" + device.getAddress()); mDeviceList.add(device); } } adapter.notifyDataSetChanged(); } //打开蓝牙 private boolean openBtDevice() { // 获得蓝牙匹配器 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // 蓝牙设备不被支持 if (mBluetoothAdapter == null) { Log.e(TAG, "Your device is not support Bluetooth!"); Toast.makeText(this, "该设备没有蓝牙设备", Toast.LENGTH_LONG).show(); return false; } // 使能蓝牙设备 if (!mBluetoothAdapter.isEnabled()) { // 隐式Intent Intent enableBtIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUES_BT_ENABLE_CODE); } else { Toast.makeText(this, "蓝牙打开成功!", Toast.LENGTH_SHORT).show(); } return true; } // 当startActivityForResult启动的 画面结束的时候,该方法被回调 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUES_BT_ENABLE_CODE && resultCode == RESULT_OK) { Toast.makeText(this, "蓝牙打开成功!", Toast.LENGTH_SHORT).show(); } } } |