1.与蓝牙相关的API
BluetoothAdapter该类的对象代表了本地的蓝牙适配器
BluetoothDevice 代表了一个远程的Bluetooth设备
2.扫描以配对的蓝牙设备
在清单文件中生命蓝牙权限
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN"/>
3.手机和电脑配对只能通过手动来完成,个人没有找到用代码实现的方法
可能由于安全性问题
4.扫描以配对的蓝牙设备(2)
1.获得BluetoothAdapter对象;
2.判断当前设备中是否拥有蓝牙设备;
3.判断当前设备中的蓝牙设备是否已经打开;
4.得到所有已经配对的蓝牙设备;
下面例子:打开手机蓝牙设备,扫描周围远程蓝牙设备,并将它们的地址显示到listview上
主Activity如下
public class BluetoothDemoActivity extends Activity {
private Button btnSet ;
private Button btnScan;
private static ArrayList<Map<String, String>> array;
private HashMap<String, String> map;
private BluetoothAdapter bluetoothAdapter;
private ListView lv;
SimpleAdapter sa;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSet = (Button) this.findViewById(R.id.btnSet);
btnScan = (Button) this.findViewById(R.id.btnScan);
lv = (ListView) this.findViewById(R.id.lv);
array = new ArrayList<Map<String,String>>();
map = new HashMap<String, String>();
IntentFilter intentFilter= new IntentFilter(BluetoothDevice.ACTION_FOUND);
BluetoothReceiver bluetoothReceiver = new BluetoothReceiver();
btnSet.setOnClickListener(new DiscoverButtonListener());
registerReceiver(bluetoothReceiver, intentFilter);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
btnScan.setOnClickListener(new ScanButtonListener());
//在listview上显示扫描出来的蓝牙设备
showListView();
}
private class BluetoothReceiver extends 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);
System.out.println(device.getAddress());
map = new HashMap<String, String>();
map.put("address", device.getAddress());
array.add(map);
sa.notifyDataSetChanged();//刷新listview
}
}
}
private void showListView() {
sa = new SimpleAdapter(getApplicationContext(), array, R.layout.bluetoothitem, new String []{"address"}, new int []{R.id.address});
lv.setAdapter(sa);
}
//修改蓝牙设备可见性
private final class DiscoverButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//设置500秒,其实最大值为300秒,超过后还是300秒
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,500);
startActivity(discoverableIntent);
}
}
private class ScanButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
//比较耗时,扫描周围蓝牙设备每次至少需要12秒,功耗比较大
//每扫描到一个蓝牙设备,就会发出一个广播
//startDiscovery这个函数是异步调用
bluetoothAdapter.startDiscovery();
}
}
}
main.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnSet"
android:text="设置可见性"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnScan"
android:text="开始扫描"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/lv"/>
</LinearLayout>
listview中的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/address"/>
</LinearLayout>