We live in a superconnected world, where 1.3 million Android devices are activated a day, and it is becoming more usual to see
users with one or even more Android devices. Seamlessly sharing data between devices might be what differentiate your app among its
competitors! Find out the potential you can unveil by getting the most of NFC, Bluetooth or WiFi Direct APIs.
5. Clases clave
Descubrir
Bluetooth Bluetooth
Adapter Device
Bluetooth
Crear
Adapter
Conectar Bluetooth
Bluetooth
Aceptar Server
Socket
Socket
6. Activando el Bluetooth
BLUETOOTH
BLUETOOTH_ADMIN
mBtAdapter =
BluetoothAdapter.getDefaultAdapter();
mBtAdapter == null?
Si
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_ENABLE_BT);
15. Escribir en una Tag
private void write(Tag tag) throws IOException,
FormatException {
NdefRecord[] records = { createRecord() };
NdefMessage message = new NdefMessage(records);
// Get an instance of Ndef for the tag.
Ndef ndef = Ndef.get(tag);
// Enable I/O
ndef.connect();
// Write the message
ndef.writeNdefMessage(message);
// Close the connection
ndef.close();
}
16. Android Beam
public class BluetoothChat extends Activity implements
CreateNdefMessageCallback{
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
try {
NdefRecord[] records = { createRecord() };
NdefMessage message = new NdefMessage(records);
return message;
} catch (UnsupportedEncodingException e) {
//Handle error here
}
return null;
}
21. Requisitos Wi-Fi Direct
Ice Cream Permisos
Sandwich
ACCESS_WIFI_STATE
CHANGE_WIFI_STATE
CHANGE_NETWORK_STATE
INTERNET
ACCESS_NETWORK_STATE
22. Con un receiver voy sobrado
WIFI_STATE_CHANGED_ACTION WIFI_P2P_PEERS_CHANGED_ACTION
WIFI_P2P_CONNECTION_CHANGED_ACTION WIFI_P2P_THIS_DEVICE_CHANGED_ACTION
23. Y esto es todo lo que hay que saber
Listener Métodos Eventos
connect(), onSuccess()
ActionListener
cancelConnect(),... onFailure()
ChannelListener initialize() onChannelDisconnected()
ConnectionInfoListener requestConnectInfo() onConnectionAvailable()
GroupInfoListener requestGroupInfo() onGroupInfoAvailable()
PeerListListener requestPeers() onPeersAvailable()
24. ¡Peer a la vista!
mManager = (WifiP2pManager)
getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);
mReceiver = new WiFiDirectBroadcastReceiver(mManager,mChannel,this);
mManager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
//success logic
}
@Override
public void onFailure(int reasonCode) {
//failure logic
}
});
25. Mientras en el receiver...
if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
if (mManager != null) {
mManager.requestPeers(mChannel, new PeerListListener(){
@Override
public void onPeersAvailable(WifiP2pDeviceList list) {
//Update Peer List, data model, etc...
}
});
}
}
26. Frecuencias de llamadas abiertas
WifiP2pDevice device = new WifiP2pDevice();
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = device.deviceAddress;
mManager.connect(mChannel, config, new ActionListener() {
@Override
public void onSuccess() {
//ready for connecting through a socket
}
@Override
public void onFailure(int reason) {
//failure logic
}
});