I am developing a bluetooth app for android on the 2.2 platform. I have two instances of mArrayAdapter in my code, and both of them cannot be resolved. My code is almost exactly the same as the code on the Android dev site, because I used it as an example. I have tried defining the variables locally, but then I get an error on .add after mArrayAdapter. I have found articles that have similar issues, but none of their answers have worked for me. I think that I have to define it somewhere, but there is not any notation about that I have found online. I will paste some of by code below. Thanks.
//Find the paired Devices
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
//If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
//---------------------> ERROR BELOW <------------------------
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
// Discovering Bluetooth Devices.
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive (Context context, Intent intent) {
String action = intent.getAction();
//When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//Log.v("bluetooth Tesing",device.getName() + "\n" + device.getAddress());
// Add the name and address to an array adapter to show in ListView.
//---------------------> ERROR BELOW <------------------------
mArrayAdapter.add(device.getName() +"\n" + device.getAddress());
}
}
};
In the Sample Bluetooth Chat, DeviceListActivity which you are using as a templete, you will see that the two ArrayAdapter adapters are both declared as member variables in the class (hence the 'm' prefix) near the top of the class. They are both instantiated with
m?????ArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
in the onCreate. You need to do something analagous for your one adapter before trying to add an item to it.
Related
I am trying to make an android application about localization, i am trying however to make a list of Bluetooth devises found by my smartphone and refresh that lists information every 10 seconds. I want to do that because I am also recording the signal strength (RSSI) and want to record when a beacon is getting out of range when moved or the signal is getting weaker. So refreshing the list will help with that. I have provided some of my code below.
#Override protected void onResume() {
super.onResume();
registerReceiver(devicesFoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
registerReceiver(devicesFoundReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED));
registerReceiver(devicesFoundReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
}
private final BroadcastReceiver devicesFoundReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int rssid = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
listAdapter.add(device.getName() + "\n" + device.getAddress() + "\n" + " RSSI: " + rssid + "dBm");
listAdapter.notifyDataSetChanged();
}else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
scanningButton.setText("Scan Bluetooth Devices");
}else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
scanningButton.setText("Scanning in progress ...");
}
}
};
use a listview then call notifyDataSetChanged() on your Adapter object
I am creating an app where I need to display the name of connected bluetooth headset . My headset is switched on and connected to android device . I am routing phone audio to headset , but I am unable to display the connected headset name .
I tried with " getName () " method , but it returns another paired bluetooth mobile device which is not currently connected and switched off .
Need suggestions so badly .
UPDATE
I used this code . But unfortunately it returns an android bluetooth device name which is not currently connected , where my headset is still connected and I am able to route phone audio
bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.cancelDiscovery();
Set<BluetoothDevice> getDevices = bluetoothAdapter.getBondedDevices();
String remoteDevice = null;
for (BluetoothDevice getDevice : getDevices) {
//stringBuilder.append(getDevice);
remoteDevice = getDevice.toString();
}
blueToothDevice = bluetoothAdapter.getRemoteDevice(remoteDevice);
StringBuilder stringBuilder1 = new StringBuilder();
stringBuilder1.append(blueToothDevice.getName());
I finally solved the problem . Previously I got all bonded devices with " getBondedDevices() " method from " BluetoothAdpter " class . But I fixed the problem by using "getConnectedDevices" method from the class " BluetoothProfile ".
My new code is below, which only shows the connected bluetooth headset device name which only connected to HEADSET profile .
bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.cancelDiscovery();
bluetoothAdapter.getProfileProxy(this, listener, BluetoothProfile.HEADSET);
public final BluetoothProfile.ServiceListener listener = new BluetoothProfile.ServiceListener() {
#Override
public void onServiceConnected(int i, final BluetoothProfile bluetoothProfile) {
final TextView txt = (TextView) findViewById(R.id.textView);
List<BluetoothDevice> b = bluetoothProfile.getConnectedDevices();
StringBuilder stringBuilder = new StringBuilder();
for(BluetoothDevice getConnectedDevice : b){
stringBuilder.append(getConnectedDevice.getName());
}
txt.setText(stringBuilder);
}
#Override
public void onServiceDisconnected(int i) {
final TextView txt = (TextView) findViewById(R.id.textView);
txt.setText(String.valueOf(i));
}
};
My Application required bluetooth connectivity. And in the first phase I am trying to open up the standard Activity "Bluetooth Device Picker" to help user scan for new device or chose a device from the list.
The problem is that I am unable to get any working example for the bluetooth device picker. The task is simple. To start an Activity with Intent "android.bluetooth.devicepicker.action.LAUNCH"
And the device picker is opening without any problem.
But the device picker requires four extras and I am unable to figure out the exact parameters for two of the extras listed below.
.putExtra("android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE","com.extreme.controlcenter"
.putExtra("android.bluetooth.devicepicker.extra.DEVICE_PICKER_LAUNCH_CLASS","com.extreme.controlcenter.WelcomeActivity")
What I thought the parameters should be that
*"android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE"*
should have the name of my package, so I passed that only. That is "com.extreme.controlcenter"
The second should be the name of the component that must receive the broadcast that is done after a device is selected. Here I tried putting the name of the class that has the onReceive() function.
But the problem is that the onReceive() function is NOT getting called when a device is picked in device picker!
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//Device Selected on Device Picker
if("android.bluetooth.devicepicker.action.DEVICE_SELECTED".equals(action)) {
//context.unregisterReceiver(this);
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(context, "device" + device.getAddress(), Toast.LENGTH_SHORT).show();
String MAC = device.getAddress();
//Log.d("my", MAC);
Intent intent2 = new Intent(WelcomeActivity.this, ControlActivity.class);
intent2.putExtra(EXTRA_DEVICE_ADDRESS, MAC);
startActivity(intent2);
}
};
I have created an Intent filter and registered a receive in the onCreate() of the main Activity
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter("android.bluetooth.devicepicker.action.DEVICE_SELECTED");
registerReceiver(mReceiver, filter);
One thing is that if I don't provide those two extras, the Broadcast event is received successfully. But that code only runs on my TAB, but same is crashing in cell phone. So I think providing those two extras are mandatory.
Thanks in Advance !
"com.extreme.controlcenter.WelcomeActivity" in your EXTRAs needs to be a BroadcastReceiver class such as MyBroadcastReceiver.class.getName(). I also have it declared in the manifest inside the tags
I'm using code from the Android developer website to detect Bluetooth devices in range, and adding them to an ArrayAdapter. Problem is, each device gets added to the ArrayAdapter 5-6 times. Right now, I'm just using the code from here: http://developer.android.com/guide/topics/connectivity/bluetooth.html#DiscoveringDevices
Here's what I have:
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery();
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};
Any idea what's causing this? And what can I do to get a device to be added only once to the ArrayAdapter, instead of 5 times?
I'm not sure if its a bug or what but I've also experienced this on some of my devices. To solve this, add found devices in a List only once with some checks. See below:
private List<BluetoothDevice> tmpBtChecker = new ArrayList<BluetoothDevice>();
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery starts
if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
//clearing any existing list data
tmpBtChecker.clear();
}
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter
if(!tmpBtChecker.contains(device)){
tmpBtChecker.add(device);
mArrayAdapter.add(device.getName()+"\n"+device.getAddress());
}
}
}
};
I am implementing Bluetooth support via implementing a task which searches for devices in the background and provides a list when the search is complete. However, this list occasionally contains a No devices found entry (added only when ACTION_DISCOVERY_FINISHED is received with no devices already in the list) before listing other devices!
private BroadcastReceiver mBlueToothInfoDiscoveryListener = new BroadcastReceiver() {
/**
* This is an overridden function called by the framework whenever there
* is some broadcast message for Bluetooth info discovery listener
*/
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been listed
// already
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
mNewBtDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
// When discovery is finished, change the Activity title
setProgressBarIndeterminateVisibility(false);
setTitle("device list");
if (mNewBtDevicesArrayAdapter.getCount() == 0) {
String noDevices = "No devices found";
mNewBtDevicesArrayAdapter.add(noDevices);
}
}
}
};
I don't expect ACTION_DISCOVERY_FINISHED to ever come before a ACTION_FOUND event, so why would the No devices found string be added to the list before a device is located?
Simply because the discovery task has a timeout to prevent endless searched. So, in case there is no available device around, you'll get your "No Devices found"...