Hi all
I'm working on a research project and I need to have a system service
running as an android component and one of it's features is performing
bluetooth scans in the environment.
I have the following code to discover bluetooth devices (taken from
BluetoothChat example). I tried it as an application and it worked but
once I'm adding it as a feature of my system service it does not work.
I'm not sure if I'm declaring the intents properly or if I need to set
up the permissions for my system service somewhere. Does any of you
tried to do something similar?
The erros I get are:
E/ErdMngr( 88): Error starting enable BT intent: No Activity found
to handle Intent { act=android.bluetooth.adapter.action.REQUEST_ENABLE
flg=0x10000000 }
E/ErdMngr( 88): Error starting enable BT intent: No Activity found
to handle Intent { act=android.bluetooth.adapter.action.REQUEST_ENABLE
flg=0x10000000 }
E/ErdMngr ( 88) Error ensuring BT device is discoverable: No Activity
found to handle Intent
{ act=android.bluetooth.adapter.action.REQUEST_DISCOVERABLE
flg=0x10000000 (has extras) }
D/ErdMngr( 88): Bluetooth discovering
I/ActivityManager( 88): Starting activity: Intent
{ act=android.bluetooth.adapter.action.REQUEST_DISCOVERABLE
flg=0x10000000 (has extras) }
And the code:
//In run (for the thread), I define the intents:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ALARM_WAKEUP);
intentFilter.addAction(ALARM_TIMEOUT);
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
mContext.registerReceiver(mBroadcastReciever, intentFilter);
//And then it calls a method to start bluetooth
private void loadInterfaces(){
Log.d(TAG, "Loading interfaces");
//At this point, interfaces must be ready
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
try{
//Enable interface
enableBluetooth();
}
catch(Exception e){
Log.e(TAG, "Failed to enable interface");
}
try{
//Ensure interface is discoverable
ensureBluetoothDiscoverability();
}
catch(Exception e){
Log.e(TAG, "Failed to set BT interface discoverable");
}
}
private void enableBluetooth(){
Log.d(TAG, "enableBluetooth()");
if(!mBtAdapter.isEnabled()){
Intent enableBtIntent = new Intent
(BluetoothAdapter.ACTION_REQUEST_ENABLE);
enableBtIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try{
mContext.startActivity(enableBtIntent);
}
catch(Exception e){
Log.e(TAG, "Error starting enable BT intent:
"+e.getMessage());
}
}
else{
Log.e(TAG, "BT Interface already enabled");
}
}
private void ensureBluetoothDiscoverability(){
Log.d(TAG, "Ensuring bluetoot is discoverable");
if(mBtAdapter.getScanMode()!=
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE){
Log.e(TAG, "Device was not in discoverable mode");
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
300);
//Without specifying the flags, it returns the
exception: "Calling
startActivity() from outside of an Activity context requires the
FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?"
discoverableIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try{
mContext.startActivity(discoverableIntent);
Log.d(TAG, "Device set up as discoverable");
}
catch(Exception e){
Log.e(TAG, "Error ensuring BT device is
discoverable:
"+e.getMessage());
}
}
else{
Log.e(TAG, "Device already discoverable");
}
}
//TODO: It should specify which interface to use (argument)
private void startDiscovery(){
if (mBtAdapter.isDiscovering()){
Log.e(TAG, "BT Interface already discovering.
Cancelling");
mBtAdapter.cancelDiscovery();
}
ensureBluetoothDiscoverability();
try{
mBtAdapter.startDiscovery();
Log.d(TAG, "Bluetooth discovering");
}
catch(Exception e){
Log.e(TAG, "Error when starting bluetooth discovery:
"+e.getMessage());
}
}
I've tried both with an HTC G1 and a Nexus one and in both cases I get
the same error (so it's not a platform issue as I initially thought).
I guess that there's an specific difference of using bluetooth as if
it were a System Service compared to an standard app (there's no
manifest for the permissions?). The Bluetooth indicator never appears
in the screen as with the application.
Any indication will be very welcome!
Many thanks,
N
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en