WiFi_P2P_PEERS_CHANGED not broadcasting after discoverPeers method - android

I followed almost exactly the Google Tutorial here https://developer.android.com/guide/topics/connectivity/wifip2p.html up to the Discovering Peers section (with the only change being that I put mChannel as an input param for the discoverPeers method as a variable named 'channel' wasn't created), and I find that onSuccess gets called but no broadcast is sent to the receiver.
I assume the receiver is initialised as it broadcasts WIFI_P2P_STATE_CHANGED_ACTION where I was also able to check that my device is WiFi P2P enabled (I'm using a Oneplus 3 running on Android Nougat) as well as WIFI_P2P_CONNECTION_CHANGED_ACTION and WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.
Help is much appreciated!

Related

How to know if my App is connected to a device using BLE in Flutter?.. when app is the beacon

So my android app is behaving as a beacon, means, it will be advertising and other BLE devices will be connecting to it. Well, this is how our project is working so please don't raise questions on this as why am i using my app as a beacon and not as a scanner. Anyways, It behaves as a beacon and starts advertising and now I want to know that if a device connected to it. I cant find a way how to do this.
Of course, I am using this flutter package. beacon_broadcast 0.3.0
This is my code.
void startAdvertising() {
BeaconBroadcast beaconBroadcast = BeaconBroadcast();
beaconBroadcast
.setUUID(advertisingUUID)
.setMajorId(1)
.setMinorId(100)
.start();
}
First, Flutter is just a UI toolkit and has no support for other system APIs such as Bluetooth.
You should therefore look what the official Android APIs offer in the first place. Usually when using BluetoothLeAdvertiser for advertising, one often also adds an instance of BluetoothGattServer in order to handle connections. If you have created a BluetoothGattServer using openGattServer, you will get a onConnectionStateChange callback whenever a device connects or disconnects. So that answers your question how an Android app can get notified when a device connects. You probably also want to use the same API to add a GATT service so that the other device can communicate with your app. Other alternatives is to use the GATT client API if it's the other device that has a GATT server, or you might want to use the L2CAP CoC API.
Note that if Bluetooth is turned off/disabled/restarted, your BluetoothGattServer object will automatically die and you need to recreate it. To get notified when this happens, use a state change intent receiver for BluetoothAdapter.ACTION_STATE_CHANGED as explained in this example https://stackoverflow.com/a/9694138/556495 to recreate your BluetoothGattServer (and advertiser) when state is changed to STATE_ON.
Now, since you want to use Flutter but Flutter uses Dart, you cannot directly consume the Android APIs. Instead you need to write a bridge/plugin, to bridge your Dart code and Java code. See https://docs.flutter.dev/development/platform-integration/platform-channels for a tutorial how to do this. If you're lucky, someone else might have already created such a package that does exactly what you want. Unfortunately, the beacon_broadcast package you found, only implements BluetoothLeAdvertiser and not BluetoothGattServer, as can be seen by the source code here: https://github.com/pszklarska/beacon_broadcast/tree/master/android/src/main/kotlin/pl/pszklarska/beaconbroadcast.

Bluetooth LE: Send "Handle Value Notification" to device

I'm trying to reproduce an ATT packet being sent to a Bluetooth LE device already connected to a phone.
Is this even possible?
I've figured out how the packet should look like, I'm just not able to wrap my head around GATT and how it translates to ATT in this specific case.
The wireshark dump looks like this:
localhost () remote () ATT 26 Sent Handle Value Notification, Handle: 0x0035 (Unknown)
Bluetooth Attribute Protocol
Opcode: Handle Value Notification (0x1b)
0... .... = Authentication Signature: False
.0.. .... = Command: False
..01 1011 = Method: Handle Value Notification (0x1b)
Handle: 0x0035 (Unknown)
Value: 0a0b313233343536373839303132
I've already figured out that the actual application payload here starts after the handle 0x0035, with 0x0a 0x0b being some custom control characters and everything afterwards a number (123456789012), exactly as I expected it to be.
How do I translate the "handle" to the GATT protocol? Is this done in Android GATT via UUIDs? If I loop over all services on that device a get a bunch of services, loads of characteristics, descriptors and I'm lost.
Oh, what I forgot to mention: None of the services and characteristics I get seem to be related to the handle 0x0035.
Is "getInstanceId" supposed to return the handle that will be used? Writing to writable characteristics on that device lead to Wireshark showing the handle value corresponding to the getInstanceId.
first thing you have to know is that in Android you can't use the Handles direktly as a developer. You have to deal with the UUIDs. in Wireshark when you have a look at the ATT Protocoll you should be able to see the UUID of the service and the characteristic when opening the Handle drop down. With this Information you should be able to detect the UUids you need.
https://developer.android.com/guide/topics/connectivity/bluetooth-le shows you how to send a value to a notification. In general you can use Constants for example to ebanle notifications. I guess if your server requires specific bytes you should be able to send them the same way.
Well, after inspecting logs and docs a little more I came to the conclusion that the remote device (which should provide serivces) here is actually using a server on the local host and this is a value notification sent from the host to the remote.

Android- wifiDirect

I am developing a simple wifiDirect chat app.My app does peer searching and once peer's list is available one can select a particular device, the one to connect with. Then my code calls connect() which sends a notification to the other device (2nd phone) about connection (default). Once the second phone agrees to the connection then 1st phone becomes group owner while second phone running my app does nothing.
So my question is do I need to call connect() on both devices? Also when is this intent action WIFI_P2P_THIS_DEVICE_CHANGED_ACTION broadcast?
Need help, read android.developers.com and other official sites but couldn't figure out.
No, you only need to call connect() on one device. Note that this does not mean that the device that calls connect() will be the group owner. The system will decide the group owner itself. You can only suggest your preference through config.groupOwnerIntent = x; where x is from 0 to 15, 0 representing least intention to become owner while 15 represents the highest.
WIFI_P2P_THIS_DEVICE_CHANGED_ACTION event will be fired when the status of the device has changed.
Use the following code to fetch the updated details of the device.
(WifiP2pDevice)intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));

Android bluetooth: how to poll for connections/disconnections

I need to continuously poll for getting bluetooth devices connections/disconnections in my activity to update a listView with the currenctly available devices.
I use btAdapter.startDiscovery() but it is not permanent ... how can i correctly get the on/off events for the devices?
I would suggest using a broadcastreceiver to listen for the specific events you are talking about. You could even fire off another discovery mode after it comes out of its current discovery mode to have it keep scanning
BluetoothAdapter.ACTION_DISCOVERY_FINISHED
BluetoothDevice.ACTION_ACL_CONNECTED
BluetoothDevice.ACTION_ACL_DISCONNECTED
You can use the intent extra's to be able to get the name (ect) from the device that connects
I would read http://developer.android.com/guide/topics/wireless/bluetooth.html and
http://developer.android.com/reference/android/content/BroadcastReceiver.html

Android 2.1 Detect Bluetooth audio connect/disconnect

I'm scratching my head trying to find a way to detect bluetooth headset connect and disconnect events for Android 2.1. I see in API Level 11 that there are some explicit ones, but how do I do it in API level 7? I just want to know when the user connects or disconnects a headset or car stereo capable of playing audio, so that I can pause the sounds I'm playing.
There is no public APIs,
This answer might help where the author used private APIs using reflections.
The author has also posted a comment on how he got it to work.
This looks like a good option to detect bluetooth connect/disconnect.
If that didn't work, another good option is to set a timer in a service that calls AudioManager.isBluetoothA2dpOn() to check if the bluetooth is connected or disconnected.
Not sure if this works in 2.1, but it works in 2.2 and 2.3.
It will capture Bluetooth-Headset connection state changes:
Declare the following intent-filter
<intent-filter >
<action android:name="android.bluetooth.headset.action.AUDIO_STATE_CHANGED" />
</intent-filter>
and in your Receiver in onReceive check for:
if ("android.bluetooth.headset.action.AUDIO_STATE_CHANGED".equals(intent.getAction())) {
headsetAudioState = intent.getIntExtra("android.bluetooth.headset.extra.AUDIO_STATE", -2);
}
and save the int as a static variable. Access it anytime you want to know if BT audio is connected(1) / disconnected(0). Not pretty, but gets the job done.
Also check out:
https://github.com/android/platform_frameworks_base/blob/gingerbread/core/java/android/bluetooth/BluetoothHeadset.java
You have to setup a BroadcastReceiver for android.bluetooth.headset.action.STATE_CHANGED action. The Intent's extra android.bluetooth.headset.extra.STATE contains current state (disconnected, connecting, connected). More info the Android source code

Categories

Resources