Android Wi-Fi Manager and Bluetooth Connection
-
Upload
jussi-pohjolainen -
Category
Technology
-
view
4.456 -
download
1
Transcript of Android Wi-Fi Manager and Bluetooth Connection
Android Wi-‐Fi and Bluetooth
Jussi Pohjolainen
WI-‐FI MANAGER
WifiManager
• WifiManager is able – List current configured networks – Can configure the current ac?ve wi-‐fi connec?on – Results of access point scans,
• Needs android.permission.ACCESS_WIFI_STATE
Show Wi-‐Fi Informa?on
WifiManager’s methods
• addNetwork(WifiConfigura?on wc) – Add a new network descrip?on to the set of configured networks.
• disableNetwork(int netId) – Disable a configured network.
• List<WifiConfigura?on> getConfiguredNetworks() – Return a list of all the networks configured in the supplicant.
• See: hTp://developer.android.com/reference/android/net/wifi/WifiManager.html
BLUETOOTH API
Android Bluetooth API
• Wirelessly exchange data with other bluetooth devices
• Possibili?es – Scan other devices – Connect to other devices through discovery – Transfer data – Manage mul?ple connec?ons
Bluetooth Permissions
• In order to use bluetooth features, one must declare at least one of the following: – BLUETOOTH • Reques?ng, accep?ng, and transferring data
– BLUETOOTH_ADMIN • Device discovery and manipulate bluetooth se\ngs
Se\ng Up Bluetooth
Se\ng Up Bluetooth
Querying Paired Devices
Discovering All Devices
• To discover use, startDiscovery from BluetoothAdapter: – mBluetoothAdapter.startDiscovery();
• Will return boolean value if the discovering started ok.
• Discovery is asynchronous and last about 12 secs
• When devices are discovered broadcast happens…
Listening to Broadcasts
• You must create a broadcast receiver that listens when devices are discovered
• Register – IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
– registerReceiver(mReceiver, filter); – // Don't forget to unregister during onDestroy
Broadcast Receiver // Create a BroadcastReceiver for ACTION_FOUND
private 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());
}
}
};
Connec?ng
• You must have a client and a server • Use classes BluetoothServerSocket and BluetoothSocket
• Connec?on is done in separate thread • You have to have server and client
Server
Client