このユースケースは、最近リリースされた Nearby API に適している可能性があります。Nearby Messages開発者の概要を見る
Nearby には独自のランタイム権限があり、マニフェストに BLUETOOTH_ADMIN などを追加する必要がありません。複数のテクノロジー (クラシック Bluetooth、BLE、超音波) を利用することで、iOS と Android で動作します。範囲を約 5 フィートに減らす超音波モデムのみを使用するオプションがあります。
以下に部分的な例を含めました。より完全なサンプルはgithubで見つけることができます
// Call this when the user clicks "find players" or similar
// In the ResultCallback you'll want to trigger the permission
// dialog
Nearby.Messages.getPermissionStatus(client)
.setResultCallback(new ResultCallback<Status>() {
public void onResult(Status status) {
// Request Nearby runtime permission if missing
// ... see github sample for details
// If you already have the Nearby permission,
// call publishAndSubscribe()
}
});
void publishAndSubscribe() {
// You can put whatever you want in the message up to a modest
// size limit (currently 100KB). Smaller will be faster, though.
Message msg = "your device identifier/MAC/etc.".getBytes();
Nearby.Messages.publish(googleApiClient, msg)
.setResultCallback(...);
MessageListener listener = new MessageListener() {
public void onFound(Message msg) {
Log.i(TAG, "You found another device " + new String(msg));
}
});
Nearby.Messages.subscribe(googleApiClient, listener)
.setResultCallback(...);
}
免責事項Nearby API に取り組んでいます