一、蓝牙的打开方式(两种)

方式一(授权打开):

Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);

方式二(静默打开):

.getDefaultAdapter();
mBluetoothAdapter.enable();

注意权限:

<uses-permission android:name="android.permission.BLUETOOTH"
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"

二、扫描设备

Demo1

MainActivity

public class BlueToothDemoOne extends Activity
private Button mButtonBluetooth;
private TextView mTextViewDeviceBonded;
private TextView mTextViewDeviceSearch;

private BluetoothAdapter mBluetoothAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bluetoothone);
mButtonBluetooth = (Button) findViewById(R.id.buttonbluetooth);
mTextViewDeviceBonded = (TextView) findViewById(R.id.textview_bonded);
mTextViewDeviceSearch = (TextView) findViewById(R.id.textview_search);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 获取之前配对过的设备
Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();

if (devices != null) {
for (BluetoothDevice device : devices) {
mTextViewDeviceBonded.append("" + device.getName() + ":"
+ device.getAddress());
}
}

mBluetoothAdapter.enable();
// 搜索到设备时发送广播
IntentFilter intentfilter = new IntentFilter(
BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, intentfilter);
// 搜索完成时发送广播
intentfilter = new IntentFilter(
BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, intentfilter);

}

public void click_start(View v) {
// 正在搜索时点击按钮将停止搜索
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
mButtonBluetooth.setText("开始搜索");

} else {
// 开启搜索设备
mBluetoothAdapter.startDiscovery();
mButtonBluetooth.setText("正在搜索");
}

}

// 接收广播
private BroadcastReceiver mReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
//根据不同的Intent.getAction进行分类
if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) {
//对搜索到的设备进行显示
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
mTextViewDeviceSearch.append(device.getName() + ":"
+ device.getAddress() + "\n");
}
}
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(intent
.getAction())) {
mTextViewDeviceSearch.append("搜索完毕");
mButtonBluetooth.setText("开始搜索");
}
}

};

}

布局:

<?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="match_parent"
android:orientation="vertical"

<Button
android:id="@+id/buttonbluetooth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click_start"
android:text="开始搜索"

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="已配对的设备"

<TextView
android:id="@+id/textview_bonded"
android:layout_width="match_parent"
android:layout_height="wrap_content"

<View
android:layout_width="match_parent"
android:layout_height="2px"
android:background="#00f"

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="搜索到的设备"

<TextView
android:id="@+id/textview_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"

</LinearLayout>