帮我写一个蓝牙连接并能接收数据的APP
时间: 2024-05-16 12:14:06 浏览: 19
好的,下面是一个简单的蓝牙连接并能接收数据的APP的实现步骤:
1. 在AndroidManifest.xml文件中添加以下权限:
```
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
```
2. 在布局文件中添加一个按钮,用于连接蓝牙:
```
<Button
android:id="@+id/btn_connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect"/>
```
3. 在Activity中实现蓝牙连接和数据接收的逻辑:
```
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter mBluetoothAdapter;
private BluetoothSocket mBluetoothSocket;
private InputStream mInputStream;
private Button mBtnConnect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnConnect = findViewById(R.id.btn_connect);
mBtnConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
connectBluetooth();
}
});
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Device does not support Bluetooth", Toast.LENGTH_SHORT).show();
finish();
return;
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
private void connectBluetooth() {
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals("Your Bluetooth Device Name")) {
try {
mBluetoothSocket = device.createRfcommSocketToServiceRecord(UUID.fromString("Your UUID"));
mBluetoothSocket.connect();
mInputStream = mBluetoothSocket.getInputStream();
startListening();
Toast.makeText(this, "Bluetooth connected", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "Failed to connect Bluetooth", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
break;
}
}
} else {
Toast.makeText(this, "No paired Bluetooth devices found", Toast.LENGTH_SHORT).show();
}
}
private void startListening() {
new Thread(new Runnable() {
@Override
public void run() {
byte[] buffer = new byte[1024];
int bytes;
while (true) {
try {
bytes = mInputStream.read(buffer);
final String message = new String(buffer, 0, bytes);
runOnUiThread(new Runnable() {
@Override
public void run() {
// Handle received message here
}
});
} catch (IOException e) {
break;
}
}
}
}).start();
}
}
```
在上面的代码中,connectBluetooth()方法用于连接蓝牙设备,startListening()方法用于启动一个新的线程来监听蓝牙设备发送的数据。你需要将"Your Bluetooth Device Name"和"Your UUID"替换为你所连接的蓝牙设备的名称和UUID。
希望这个简单的示例可以帮到你!
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)