Android模拟器不支持蓝牙技术,所以需要用真机测试。
需要在配置文件中设置有关蓝牙的权限,代码如下:
<uses-permission android;name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
BluetoothAdapter 是蓝牙API的核心类,下面的代码创建BluetoothAdapter对象
private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
打开蓝牙的二种方法:
//间接打开,会提示用户是否打开
public void open1(View v){
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent,1);
}
//粗暴打开方式,偷偷的打开
public void open(View v){
bluetoothAdapter.enable();
}
相对应的偷偷关闭蓝牙
public void close(View v){
bluetoothAdapter.disable();
}
笔者写了一个小程序,搜索蓝牙设备
源代码如下:
package com.mm.bluetooth;
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.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import java.util.Set;
public class MainActivity extends Activity {
private BluetoothAdapter bluetoothAdapter;
private TextView tvDevices;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
tvDevices = (TextView) findViewById(R.id.tv_Device);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//获得所以绑定的蓝牙设备
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
//将已经绑定的蓝牙设备的名称和地址显示在textview控件中
tvDevices.append(device.getName() + ":" + device.getAddress() + "\n");
}
}
//注册用于接收已经搜索到的蓝牙设备的Receiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(receiver, filter);
//注册搜索完成时的Receiver
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(receiver, filter);
}
//点击事件
public void onClick_Search(View view) {
setProgressBarIndeterminateVisibility(true);
setTitle("正在扫描...");
if (bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.cancelDiscovery();
}
bluetoothAdapter.startDiscovery();
}
private 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);
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
tvDevices.append(device.getName() + ":" + device.getAddress() + "\n");
}
}
else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
setProgressBarIndeterminateVisibility(false);
setTitle("搜索蓝牙设备");
}
}
};
//打开蓝牙的二种方法
public void open1(View v){
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent,1);
}
public void open(View v){
bluetoothAdapter.enable();
}
public void close(View v){
bluetoothAdapter.disable();
}
}
至于运行的结果就不在演示了,因为小编在真机上测试,不知道怎么截图了!!!
最后,祝大家代码无Bug!