![这里写图片描述](https://img-blog.csdn.net/20160807184205926)
android代码实现手机和蓝牙板块之间的通讯
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="scan"
android:text="扫描蓝牙设备" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="stop"
android:text="停止蓝牙扫描" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="switchLight"
android:text="开关电灯" />
<LinearLayout
android:id="@+id/ll_bluetooth"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</LinearLayout>
public class MainActivity extends Activity {
BluetoothAdapter mBluetoothAdapter;
BluetoothReceiver mBluetoothReceiver;
LinearLayout mLl_bluetooth;
OutputStream os;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mLl_bluetooth = (LinearLayout) findViewById(R.id.ll_bluetooth);
mBluetoothAdapter.enable();
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothDevice.ACTION_FOUND);
mBluetoothReceiver = new BluetoothReceiver();
registerReceiver(mBluetoothReceiver, filter);
}
private class BluetoothReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Toast.makeText(MainActivity.this, "开始扫描蓝牙设备", 0).show();
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
.equals(action)) {
Toast.makeText(MainActivity.this, "扫描蓝牙设备完成", 0).show();
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
Toast.makeText(MainActivity.this, "发现了蓝牙设备", 0).show();
final BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getName() + "\n"
+ device.getAddress());
TextView tv = new TextView(MainActivity.this);
tv.setText(device.getName() + "----" + device.getAddress());
tv.setTextSize(25);
tv.setTag(device);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
BluetoothSocket socket;
try {
socket = device.createRfcommSocketToServiceRecord(UUID
.fromString("00001000-0000-1000-8000-00805F9B34FB}"));
socket.connect();
os = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
});
mLl_bluetooth.addView(tv);
}
}
}
@Override
protected void onDestroy() {
unregisterReceiver(mBluetoothReceiver);
mBluetoothReceiver = null;
}
public void switchLight(View v) {
try {
os.write(0xA1);
os.write(0xF1);
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void scan(View v) {
mBluetoothAdapter.startDiscovery();
}
}
需要的权限
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>