Reputation: 557
In my application, I am using Janos Gyerik's BluetoothViewer code to connect via Bluetooth.
I also have a TCPServer
Class to connect via USB.
public void connectViaUSB() {
if (GlobalVariables.connectionstatus == 0) {
mTCPServer = new TCPServer(2222, getCurrentActivity());
mTCPServer.start();
mTCPServer.setResponseReceivedListener(this);
}
The problem is, BluetoothViewer
is written as an Activity
. One needs to start the activity to connect, but when I switch to another activity connection is lost.
I need to either keep that activity up all the time, or find a way to modify it. How can I fix this situation?
EDIT
`
public class MyService extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
mAdapter.enable();
BluetoothChatService mChatService = new BluetoothChatService(mHandler);
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:12:11:13:19:26");
mChatService.connect(device);
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
//stop thread
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
//start thread (again)
}
}`
How can I make the Bluetooth connection from this Service Class? It requires me to move mHandler and I did. Now I'm getting RuntimeException: Unable to create .MyService: NullPointerException
Upvotes: 0
Views: 5596
Reputation: 4910
Not sure if this might help you but I created a robot once that was controlled by Bluetooth. It used a connection running inside a service/thread. For this I used the following 2 classes:
BluetoothSerialService.java
DeviceListActivity.java
Just google the 2 classes and you will find a place where they are hosted. Like here. http://code.google.com/p/bluetooth-remote-control/source/browse/trunk/src/pro/apus/blueremote/?r=2
To actually setup the connection, just bind something like this to a button.
public void autoconnect() {
t = new TextView(this);
t = (TextView) findViewById(R.id.connecttext);
mSerialService = new BluetoothSerialService(this, mHandlerBT, t);
if (mSerialService.getState() == BluetoothSerialService.STATE_NONE) {
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter()
.getRemoteDevice(mMacAddress);
mSerialService.connect(device);
} else if (mSerialService.getState() == BluetoothSerialService.STATE_CONNECTED) {
setProgressBarIndeterminateVisibility(false);
mSerialService.stop();
mSerialService.start();
}
}
And make sure to ask for permission before doing that.
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
.getDefaultAdapter();
askBluetoothPermission(mBluetoothAdapter);
Upvotes: 0
Reputation: 4981
Try to use Service to connect to bluetooth. And when you will be needed use bluetooth just call service. This can help you: http://www.vogella.com/articles/AndroidServices/article.html
Upvotes: 1