导航:首页 > 操作系统 > android蓝牙串口接收数据

android蓝牙串口接收数据

发布时间:2023-05-17 22:43:04

1. android蓝牙开发,PC端模拟串口接收字符,该如何编程

您好,android蓝牙这方面还是很好搞的,因为大家的方式都是差不多的。先说说如何开启蓝牙设备和设置可见时间:

private void search() {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (!adapter.isEnabled()) {
adapter.enable();
}
Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
enable.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3600); //3600为蓝牙设备可见时间
startActivity(enable);
Intent searchIntent = new Intent(this, ComminuteActivity.class);
startActivity(searchIntent);
}

首先,需要获得一个BluetoothAdapter,可以通过getDefaultAdapter()获得系统默认的蓝牙适配器,当然我们也可以自己指定,但这个真心没有必要,至少我是不需要的。然后我们检查手机的蓝牙是否打开,如果没有,通过enable()方法打开。接着我们再设置手机蓝牙设备的可见,可见时间可以自定义。

完成这些必要的设置后,我们就可以正式开始与蓝牙模块进行通信了:

public class ComminuteActivity extends Activity {
private BluetoothReceiver receiver;
private BluetoothAdapter bluetoothAdapter;
private List<String> devices;
private List<BluetoothDevice> deviceList;
private Bluetooth client;
private final String lockName = "BOLUTEK";
private String message = "000001";
private ListView listView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_layout);

listView = (ListView) this.findViewById(R.id.list);
deviceList = new ArrayList<BluetoothDevice>();
devices = new ArrayList<String>();
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.startDiscovery();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
receiver = new BluetoothReceiver();
registerReceiver(receiver, filter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setContentView(R.layout.connect_layout);
BluetoothDevice device = deviceList.get(position);
client = new Bluetooth(device, handler);
try {
client.connect(message);
} catch (Exception e) {
Log.e("TAG", e.toString());
}
}
});
}

@Override
protected void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}

private final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Bluetooth.CONNECT_FAILED:
Toast.makeText(ComminuteActivity.this, "连接失败", Toast.LENGTH_LONG).show();
try {
client.connect(message);
} catch (Exception e) {
Log.e("TAG", e.toString());
}
break;
case Bluetooth.CONNECT_SUCCESS:
Toast.makeText(ComminuteActivity.this, "连接成功", Toast.LENGTH_LONG).show();
break;
case Bluetooth.READ_FAILED:
Toast.makeText(ComminuteActivity.this, "读取失败", Toast.LENGTH_LONG).show();
break;
case Bluetooth.WRITE_FAILED:
Toast.makeText(ComminuteActivity.this, "写入失败", Toast.LENGTH_LONG).show();
break;
case Bluetooth.DATA:
Toast.makeText(ComminuteActivity.this, msg.arg1 + "", Toast.LENGTH_LONG).show();
break;
}
}
};

private class BluetoothReceiver extends 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 (isLock(device)) {
devices.add(device.getName());
}
deviceList.add(device);
}
showDevices();
}
}

private boolean isLock(BluetoothDevice device) {
boolean isLockName = (device.getName()).equals(lockName);
boolean isSingleDevice = devices.indexOf(device.getName()) == -1;
return isLockName && isSingleDevice;
}

private void showDevices() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
devices);
listView.setAdapter(adapter);
}
}

2. android蓝牙串口通信,接收数据问题

http://stackoverflow.com/questions/16089261/how-to-convert-byte-valueto-hex-and-then-to-decimal-which-is-from-bluetooth-spp

3. Android蓝牙通信是怎么获取串口数据

代码片段上传一下
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
BluetoothServerSocket bss = BluetoothAdapter.getDefaultAdapter() .(NAME_SECURE, MY_UUID);
就可以对SOCKET进行读写操作了

4. Android手机端通过socket接收蓝牙模块串口发来的字符串,出现字符串被截断现象,求解决办法,万分感谢!

在while循环外部的上方申明:
String sda="";

将while循环里面的 String sda = new String(byte_data);
改为:sda+=new String(byte);

最后将while循环里面的 System.out.println("收到的数据sda为:"+sda);
移到while循环外部的下方。

5. Android蓝牙BLE连接如何设置串口,数据格式为含1位起始位、7位数据位、1位奇偶校验位和1位终止位

字符长度=1+7+1+2=11 b/字符
数据速率R1=11×100=1100 b/s
有效数据速率R=7×100=700 b/s
选B

阅读全文

与android蓝牙串口接收数据相关的资料

热点内容
镂空加密隔断墙效果图 浏览:539
windows7怎么安装证书服务器 浏览:952
证券业务的程序员 浏览:202
u点服务器wifi密码如何设置 浏览:864
宝马x5大灯编程 浏览:673
python安装和使用 浏览:381
加密的门禁卡复制了用不了 浏览:714
javacsv读写 浏览:806
ug编程教程pdf 浏览:763
latex编译软件安卓版 浏览:248
如何在信合app上交居民医保 浏览:109
丑恶pdf 浏览:365
陕西定频压缩机销售公司 浏览:795
安卓系统如何帮人打王者 浏览:427
sbtlinux安装 浏览:141
阿里云sip服务器 浏览:73
身为程序员的你怎么拼命 浏览:453
android图片手势放大 浏览:586
钱的所有算法 浏览:13
光模块服务器怎么直接连电脑 浏览:376