This document provides information about using Wi-Fi and Bluetooth APIs on Android. It discusses the WifiManager class for getting information about Wi-Fi networks and configuring connections. It also covers the Android Bluetooth API for discovering, connecting to, and transferring data with other Bluetooth devices through permissions, device discovery, and creating client/server sockets for connections.
3. 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
5. 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
7. 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
8. 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
sengs
12. 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…
13. 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
14. 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());
}
}
};
15. 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