Witam,
chciałbym aby moja aplikacja po przyjściu smsa, lub połączenia wysyłała jakiekolwiek dane do wcześniej połączonego innego urządzenia BT (mikrokontroler).
Probelem jaki napotkałem to przekazanie tych danych z poziomu BroadcastReceiver. Wytłumaczę schemat działania to wszystko stanie się jasne.
Działanie aplikacji:
- Uruchamianie Activity gdzie wyszukiwane jest urządzenie, tworzony jest obiekt BluetoothObject(), i następuje połączenie (połączenie z urzadzeniem jest na sztywno - podane UUID).
- Po przyjściu smsa bądź połączenia uruchamia się usługa działająca w tle próbująca wysłać dane, co jednak się nie dzieje, bo sie wykrzacza.
Poniżej załączam klasy i cały spakowany program:
Main:
package com.dynamic.sms.read;
import java.io.IOException;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class Main extends Activity {
private static final int REQUEST_ENABLE_BT = 1;
public BluetoothDevice device;
public boolean connectionState;
OnItemClickListener itemClickListener;
ListView listDevicesFound;
Button btnScanDevice;
TextView stateBluetooth;
BluetoothAdapter bluetoothAdapter;
BluetoothObject bt = new BluetoothObject();
ArrayAdapter<String> btArrayAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smsapp);
btnScanDevice = (Button) findViewById(R.id.scandevice);
stateBluetooth = (TextView) findViewById(R.id.bluetoothstate);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
listDevicesFound = (ListView) findViewById(R.id.devicesfound);
btArrayAdapter = new ArrayAdapter<String>(Main.this,
android.R.layout.simple_list_item_1);
listDevicesFound.setAdapter(btArrayAdapter);
CheckBlueToothState();
btnScanDevice.setOnClickListener(btnScanDeviceOnClickListener);
registerReceiver(ActionFoundReceiver, new IntentFilter(
BluetoothDevice.ACTION_FOUND));
listDevicesFound.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//System.out.println(device.getUuids());
btnScanDevice.setEnabled(true);
unregisterReceiver(ActionFoundReceiver);
connectBt();
finish();
}
});
}
private void CheckBlueToothState() {
if (bluetoothAdapter != null) {
if (bluetoothAdapter.isEnabled()) {
stateBluetooth.setText("Bluetooth jest włączone.");
if (bluetoothAdapter.isDiscovering()) {
btnScanDevice.setEnabled(false);
} else {
btnScanDevice.setEnabled(true);
}
} else {
stateBluetooth.setText("Bluetooth jest wyłączone!");
Intent enableBtIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
private Button.OnClickListener btnScanDeviceOnClickListener = new Button.OnClickListener() {
public void onClick(View arg0) {
btnScanDevice.setEnabled(false);
btArrayAdapter.clear();
bluetoothAdapter.startDiscovery();
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ENABLE_BT) {
CheckBlueToothState();
}
}
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
btArrayAdapter.add(device.getName() + "\n"
+ device.getAddress());
btArrayAdapter.notifyDataSetChanged();
}
}
};
public void connectBt(){
bt.mmDevice = device;
try {
bt.openBT();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BluetoothObject:
package com.dynamic.sms.read;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
public class BluetoothObject {
public BluetoothSocket mmSocket;
public BluetoothDevice mmDevice;
public OutputStream mmOutputStream;
public InputStream mmInputStream;
public Thread workerThread;
public byte[] readBuffer;
public int readBufferPosition, counter;
public double przeliczenieWartosci;
public String msg = "";
public volatile boolean stopWorker;
public boolean wlaczoneBluetooth = false;
public void openBT() throws IOException {
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // Standard
// //SerialPortService
// ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
wlaczoneBluetooth = true;
showMessage("Połaczono z EventRing");
}
public String sendData(String message) throws IOException {
if (wlaczoneBluetooth == true) {
char[] msgChar = message.toCharArray();
for (int i = 0; i <= message.length() - 1; i++) {
mmOutputStream.write(msgChar[i]);
}
showMessage("Dane zostały wysłane");
}
return message;
}
public void showMessage(String log) {
System.out.println("EventRingLOG: "+log);
}
}
IncomingSms:
package com.dynamic.sms.read;
import java.io.IOException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class IncomingSms extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras == null) {
return;
}
Toast.makeText(context.getApplicationContext(),
"Przekazano do kontrolera", Toast.LENGTH_SHORT).show();
BluetoothObject bt = new BluetoothObject();
try {
bt.sendData("0");
System.out.println("Odebrano wiadomość i przesłano do kontrolera.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
- BT.zip (837 KB) - ściągnięć: 122