Commit 4d76236f authored by Chengzhao Li's avatar Chengzhao Li
Browse files

- shorten the display name, since the max advertising data length is 31.

- add more log
- fix peripheral bugs
parent 8edd5825
......@@ -136,7 +136,7 @@ public class MultiplayerController {
initSessionCallback();
String uuid = UUID.randomUUID().toString();
String suuid = uuid.substring(24);
String suuid = uuid.substring(34);
String displayName = suuid + localName;
NCMCPeerID peerID = new NCMCPeerID(displayName);
......@@ -352,8 +352,8 @@ public class MultiplayerController {
@NonNull
public String stringForMCPeerDisplayName(String displayName) {
if (displayName != null && displayName.length() > 12) {
return displayName.substring(12);
if (displayName != null && displayName.length() > 2) {
return displayName.substring(2);
}
return "Unknown Player";
......
......@@ -181,8 +181,10 @@ import java.util.concurrent.ConcurrentHashMap;
public void disconnect() {
if (isCentral()) {
stopBrowsing();
for (NCMCPeripheralInfo info : this.mDiscoveredPeripherals.values()) {
disconnectToPeripheralByInfo(info);
if (this.mDiscoveredPeripherals != null && !this.mDiscoveredPeripherals.isEmpty()) {
for (NCMCPeripheralInfo info : this.mDiscoveredPeripherals.values()) {
disconnectToPeripheralByInfo(info);
}
}
} else {
stopAdvertising();
......@@ -668,6 +670,7 @@ import java.util.concurrent.ConcurrentHashMap;
};
private void scanLeDevice(final boolean enable) {
Log.d(TAG, "scanLeDevice: " + enable);
if (enable && !mIsBrowsingOrAdvertising) {
// Stops scanning after a pre-defined scan period.
mScanHandler.postDelayed(mScanRunnable, SCAN_PERIOD);
......@@ -676,8 +679,10 @@ import java.util.concurrent.ConcurrentHashMap;
mIsBrowsingOrAdvertising = true;
mLEScanner.startScan(mFilters, mScanSettings, mScanCallback);
} else {
mIsBrowsingOrAdvertising = false;
mLEScanner.stopScan(mScanCallback);
if (mIsBrowsingOrAdvertising) {
mIsBrowsingOrAdvertising = false;
mLEScanner.stopScan(mScanCallback);
}
}
}
......@@ -935,7 +940,7 @@ import java.util.concurrent.ConcurrentHashMap;
if (preparedWrite) {
addWriteItemByteBuffer(device.getAddress(), characteristic.getUuid().toString(), value, true);
} else {
processMsg(characteristic.getValue(), device.getAddress());
processMsg(value, device.getAddress());
}
}
}
......@@ -967,7 +972,7 @@ import java.util.concurrent.ConcurrentHashMap;
@Override
public void onNotificationSent(BluetoothDevice device, int status) {
super.onNotificationSent(device, status);
Log.d(TAG, "onNotificationSent to " + device.getName() + " status : " + status);
Log.d(TAG, "onNotificationSent to " + device.getAddress() + " status : " + status);
synchronized (mMessageSendQueue) {
Log.d(TAG, "onNotificationSent: current queue size:" + mMessageSendQueue.size());
if (mMessageSendQueue.size() != 0) {
......@@ -1039,12 +1044,12 @@ import java.util.concurrent.ConcurrentHashMap;
public void stopAdvertising() {
if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
scanLeDevice(false);
mScanHandler.removeCallbacks(mScanRunnable);
setIsAdvertise(false);
}
}
private void setIsAdvertise(boolean enable) {
Log.d(TAG, "setIsAdvertise: " + enable);
if (enable) {
mIsBrowsingOrAdvertising = true;
mAdvertiser.startAdvertising(mAdSettings, mAdData, mAdvertisingCallback);
......@@ -1102,7 +1107,7 @@ import java.util.concurrent.ConcurrentHashMap;
}
protected void notifyDidReceiveInvitationFromPeer(NCMCPeerID peerID) {
if (this.mDiscoveredPeripherals != null) {
if (this.mPeripheralService != null) {
this.mPeripheralService.notifyDidReceiveInvitationFromPeer(peerID);
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment