【发布时间】:2019-07-15 11:55:55
【问题描述】:
我正在制作一个 Android Studio 应用程序来通过蓝牙控制我的 Arduino 项目。我已使用以下类成功地将 HC-06 模块连接到应用程序:
class ConnectBT extends AsyncTask<Void, Void, Void>{
private boolean ConnectSuccess = true;
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... devices)
{
try
{
if (Drive.btSocket == null || !Drive.isBtConnected)
{
Drive.myBluetooth = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice dispositivo = Drive.myBluetooth.getRemoteDevice(Drive.BTaddress);
Drive.btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(Drive.myUUID);
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
Drive.btSocket.connect();
}
}
catch (IOException e)
{
ConnectSuccess = false;exception here
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
if (!ConnectSuccess)
{
msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
}
else
{
msg("Connected.");
Drive.loadingScreen.setVisibility(View.GONE);
System.out.println("Connected");
Drive.isBtConnected = true;
}
//Drive.progress.dismiss();
}
private void msg(String s)
{
//Toast.makeText(Drive.class.,s,Toast.LENGTH_LONG).show();
}
}
但是现在当我尝试从应用程序发送和接收数据时,事情变得很困难,所以我被卡住了。我已经用谷歌搜索了如何发送和接收数据,但我主要找到有关如何从头开始制作 BT 应用程序的教程。如何在 Android 应用程序中通过蓝牙从 HC-06 发送和接收数据?任何代码sn-ps?
【问题讨论】:
标签: java android android-studio arduino bluetooth