android studio实现串口通信
时间: 2023-09-17 18:09:18 浏览: 192
要在Android Studio中实现串口通信,您需要遵循以下步骤:
1. 配置权限:在AndroidManifest.xml文件中添加以下行:
```
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.WRITE_PROFILE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.BODY_SENSORS" />
<uses-permission android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<uses-permission android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
<uses-feature android:name="android.hardware.usb.host" />
```
2. 添加依赖项:在build.gradle文件中添加以下依赖项:
```
dependencies {
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation files('libs/usbSerialForAndroid.jar')
}
```
3. 添加代码:在您的Android Studio项目中添加以下代码:
```
public class MainActivity extends AppCompatActivity implements UsbSerialInterface.UsbReadCallback {
private static final String TAG = MainActivity.class.getSimpleName();
private UsbManager usbManager;
private UsbDevice device;
private UsbSerialDevice serialPort;
private UsbDeviceConnection connection;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text_view);
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(usbReceiver, filter);
//获取设备列表
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
if (!deviceList.isEmpty()) {
boolean keep = true;
for (UsbDevice device : deviceList.values()) {
// 判断设备VID和PID是否符合
if (device.getVendorId() == 0x1a86 && device.getProductId() == 0x7523) {
// 找到设备
this.device = device;
Log.i(TAG, "找到设备");
break;
} else {
connection = null;
this.device = null;
}
}
if (device == null) {
Log.e(TAG, "没有找到设备");
return;
}
} else {
Log.e(TAG, "没有设备");
return;
}
// 打开设备,获取 UsbDeviceConnection 对象,连接设备,用于后面的通讯
connection = usbManager.openDevice(device);
if (connection == null) {
Log.e(TAG, "连接失败");
return;
}
serialPort = UsbSerialDevice.createUsbSerialDevice(device, connection);
if (serialPort != null) {
if (serialPort.open()) { // 开启设备
//设置波特率
serialPort.setBaudRate(9600);
//设置数据位
serialPort.setDataBits(UsbSerialInterface.DATA_BITS_8);
//设置停止位
serialPort.setStopBits(UsbSerialInterface.STOP_BITS_1);
//设置校验位
serialPort.setParity(UsbSerialInterface.PARITY_NONE);
//设置流控制
serialPort.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF);
// 开启读取线程,开始接收数据
serialPort.read(this);
Log.i(TAG, "连接成功");
} else {
serialPort.close();
Log.e(TAG, "开启设备失败");
}
} else {
Log.e(TAG, "没有找到设备");
}
}
@Override
public void onReceivedData(byte[] bytes) {
// 接收到数据的回调
runOnUiThread(() -> {
String s = new String(bytes);
textView.setText(s);
});
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(usbReceiver);
if (serialPort != null) {
serialPort.close();
}
}
private BroadcastReceiver usbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
// USB设备插入
Log.i(TAG, "USB设备已插入");
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
// USB设备拔出
Log.i(TAG, "USB设备已拔出");
}
}
};
}
```
在这个示例中,我们使用了USB to TTL串口模块。请注意,您需要正确设置波特率、数据位、停止位、校验位和流控制,以便与您的设备通信。
希望这个步骤能够帮助您在Android Studio中实现串口通信。
阅读全文