Здравствуйте. Я хочу отправить двоичные файлы из Arduino в мое приложение для Android через Bluetooth.Я уже написал приложение, которое успешно устанавливает связь и может получать данные.Однако много байтов отсутствует во время связи.Странно то, что я получаю каждый байт, когда использую приложение терминала Bluetooth.Это означает, что связь в порядке, но мое приложение не вызывает событие получения или что-то в этом роде.
Мой код:
private final Semaphore sem = new Semaphore(1, true);
@Override
protected void onCreate(Bundle savedInstanceState) {
img = new ImageSaver("image2.jpg", this);
bluetoothModule = new BluetoothSPP(this);
if (!bluetoothModule.isBluetoothAvailable()) {
output.append("Bluetooth is not available on this device");
} else {
bluetoothModule.enable();
bluetoothModule.setupService();
bluetoothModule.startService(BluetoothState.DEVICE_OTHER);
bluetoothModule.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() {
public void onDeviceConnected(String name, String address) {
if(name.equals("bilderstutzen"))
output.append("Connected to bilderstutzen");
}
public void onDeviceDisconnected() {
output.append("Connection lost");
}
public void onDeviceConnectionFailed() {
output.append("Unable to connect");
}
});
bluetoothModule.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {
@Override
public void onDataReceived(byte[] data, String message) {
try{
sem.acquire();
//Log.d("save image", String.valueOf(img.length));
} catch (InterruptedException e) {
e.printStackTrace();
}
img.saveImage(data);
sem.release();
}
});
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == BluetoothState.REQUEST_CONNECT_DEVICE)
if(resultCode == Activity.RESULT_OK) {
if(data != null)
bluetoothModule.connect(data);
Log.d("connecting", "works");
}
else if(requestCode == BluetoothState.REQUEST_ENABLE_BT) {
if(resultCode == Activity.RESULT_OK) {
bluetoothModule.setupService();
//bluetoothModule.startService(BluetoothState.DEVICE_OTHER);
//setup();
} else {
output.append("bluetooth not enabled");
finish();
// Do something if user doesn't choose any device (Pressed back)
}
}
}
Mimimal Версия метода saveImage:
public void saveImage(byte[] img){
for(byte b : img){ //just prints out the received data. With around 2000KB of data Im Missing around 200bytes
Log.d("save image", String.format("%02x", b));
}
}
Надеюсь, вы поможете мне с этим вопросом!