蓝牙通信-搜索附近的蓝牙设备
来源:互联网 发布:淘宝买鲜花靠谱吗 编辑:程序博客网 时间:2024/04/30 13:49
与其他设备通信通信之前需要搜索周围的蓝牙设备。
怎么搜索呢??
1.如果数据中已经和某些蓝牙设备绑定,可以使用BluetoothAdapter.getBondedDevices();方法获得已经绑定的蓝牙设备列表
2.搜索周围的蓝牙设备受用BluetoothAdapter.startDiscovery()方法
3.搜索到的蓝牙设备都是通过广播返回,so..。需要注册广播接收器来获得已经搜索到的蓝牙设备。
下面我们看一下demo:
我们在布局文件中放一个按钮和一个显示文本TextView。
我们点击Button时,开始搜索附近的蓝牙设备。将搜索到的蓝牙设备追加到TextView上,我们将绑定的和搜索到的都显示在TextView上。
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick_Search" android:text="搜索蓝牙设备" /> <TextView android:id="@+id/tvDevices" android:layout_width="fill_parent" android:layout_below="@+id/button_id" android:layout_height="wrap_content" /></RelativeLayout>
package com.example.search_bluetooth_devices;import java.util.Set;import android.os.Bundle;import android.app.Activity;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.view.Menu;import android.view.View;import android.view.Window;import android.widget.TextView;public class MainActivity extends Activity {private TextView mTextView;private BluetoothAdapter mBluetoothAdapter;private BroadcastReceiver mReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubString action = intent.getAction();// 获得已经搜索到的蓝牙设备if (action.equals(BluetoothDevice.ACTION_FOUND)) {BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);// 搜索到的不是已经绑定的蓝牙设备if (device.getBondState() != BluetoothDevice.BOND_BONDED) {// 显示在TextView上mTextView.append(device.getName() + ":"+ device.getAddress()+"\n");}// 搜索完成} else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {setProgressBarIndeterminateVisibility(false);setTitle("搜索蓝牙设备");}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);setContentView(R.layout.activity_main);mTextView = (TextView) findViewById(R.id.tvDevices);mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();// 获取所有已经绑定的蓝牙设备Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();if (devices.size() > 0) {for (BluetoothDevice bluetoothDevice : devices) {mTextView.append(bluetoothDevice.getName() + ":"+ bluetoothDevice.getAddress() + "\n\n");}}// 注册用以接收到已搜索到的蓝牙设备的receiverIntentFilter mFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);registerReceiver(mReceiver, mFilter);// 注册搜索完时的receivermFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);registerReceiver(mReceiver, mFilter);}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();//解除注册unregisterReceiver(mReceiver);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}public void onClick_Search(View v) {setProgressBarIndeterminateVisibility(true);setTitle("正在扫描....");// 如果正在搜索,就先取消搜索if (mBluetoothAdapter.isDiscovering()) {mBluetoothAdapter.cancelDiscovery();}// 开始搜索蓝牙设备,搜索到的蓝牙设备通过广播返回mBluetoothAdapter.startDiscovery();}}
配置文件:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.search_bluetooth_devices" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.search_bluetooth_devices.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
运行结果:
本机绑定的蓝牙设备一开始就显示(第一张图),点击搜索蓝牙设备后(第2张图)
- 蓝牙通信-搜索附近的蓝牙设备
- 搜索附近的蓝牙设备
- 蓝牙通信-如果允许本地蓝牙被附近的其它蓝牙设备发现
- Android 6.0蓝牙搜索不到附近设备
- android之蓝牙通信(一)(检测蓝牙、开启蓝牙、扫描附近蓝牙设备)
- Android搜索附近蓝牙
- 蓝牙开启&& 被附近的其它蓝牙设备发现
- Android 扫描附近的蓝牙设备并连接蓝牙音响
- 搜索附近的蓝牙设备,并将其名字和mac地址现在在textview中
- 编写app安卓程序搜索手机附近的蓝牙设备
- Android 蓝牙通信开发(一)搜索蓝牙设备
- Android设备的蓝牙通信
- android 蓝牙通讯实现手机蓝牙的开启,并扫描附近可见的蓝牙设备
- 使用J2ME搜索蓝牙设备,并与搜到的设备通信
- Android 6.0 搜索不到附近蓝牙问题解决
- WM蓝牙开发--搜索周围的设备
- 如何修改蓝牙搜索设备的时间
- 修改蓝牙搜索设备的时间
- iPhone开发面试题--葵花宝典
- 用递归做螺旋矩阵 java版算法
- HEVC混合编码方法引入的亮点
- Ubuntu11.10无法打开softer center(软件中心)和language support的解决
- Scott用户4个表的表结构及字段含义
- 蓝牙通信-搜索附近的蓝牙设备
- 控制文件的添加,转移
- 2013最受女性创业项目排行榜
- jQuery获取Select option 选择的Text和Value
- 西电计算机研究生复试上机题
- MySQL 学习笔记 一
- Handler默认情况下与调用它的Activity是处于同一个线程的实例证明
- linux下创建和删除软、硬链接
- android中json解析及使用(上)