導航:首頁 > 操作系統 > 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藍牙串口接收數據相關的資料

熱點內容
qq可以加密密碼 瀏覽:854
網上認證無法連接網站伺服器地址 瀏覽:920
java資料庫建表 瀏覽:170
linux本地埠 瀏覽:36
郵票目錄pdf 瀏覽:378
解壓筆真的有用嗎 瀏覽:538
plc語法和編程語句 瀏覽:115
南航app學生旅行產品在哪裡 瀏覽:240
漫畫pdf網盤 瀏覽:495
手機pdf字太小 瀏覽:892
解壓縮安裝圖解 瀏覽:257
安卓如何查看運行伺服器 瀏覽:681
土質學pdf 瀏覽:995
郵箱收發伺服器地址 瀏覽:143
web前端和python 瀏覽:639
定址演算法 瀏覽:783
車床編程ijk之間的關系 瀏覽:493
安卓什麼app可以訪問smb 瀏覽:205
籠統概括程序員 瀏覽:853
linuxfdiskdf 瀏覽:300