Commit 0924ece8 authored by Chengzhao Li's avatar Chengzhao Li
Browse files

- fix connection bugs

parent 0f1c9ced
......@@ -9,7 +9,8 @@
android:screenOrientation="landscape"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LoginActivity">
<activity android:name=".LoginActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......
......@@ -111,7 +111,9 @@ public class MultiplayerController {
public void setContext(Context _context) {
this.context = _context;
this.currentSession.setContext(_context);
if (this.currentSession != null) {
this.currentSession.setContext(_context);
}
}
private void broadcastStatus(String action) {
......@@ -139,7 +141,7 @@ public class MultiplayerController {
String uuid = UUID.randomUUID().toString();
String suuid = uuid.substring(24);
String displayName = suuid + uuid;
String displayName = suuid + localName;
NCMCPeerID peerID = new NCMCPeerID(displayName);
this.currentSession = new NCMCSession(peerID, TRANSFER_SERVICE_UUID, this.context);
......
......@@ -16,11 +16,10 @@
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/title"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:textSize="60sp"
android:textColor="#ffffff" />
android:textColor="#ffffff"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
......@@ -30,7 +29,7 @@
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
android:layout_marginTop="30dp"
android:textColor="#ffffff" />
<EditText
......@@ -48,13 +47,12 @@
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/btnStart"
android:layout_below="@+id/userName"
android:layout_centerHorizontal="true"
android:layout_marginTop="121dp"
android:src="@drawable/buttonplay"
android:scaleType="fitCenter"
android:background="@android:color/transparent"
android:adjustViewBounds="false"
tools:ignore ="ContentDescription" />
tools:ignore ="ContentDescription"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
......@@ -354,6 +354,8 @@ import java.util.UUID;
/***********************************************************************/
public boolean setupCentralEnv(NCMCCentralService service) {
this.mCentralService = service;
this.mIsCentral = true;
if (!setup()) {
if (this.mCentralService != null) {
this.mCentralService.notifyDidNotStartBrowsingForPeers(NCMCCentralService.NCMCCentralService_ERROR_UNKNOWN);
......@@ -367,7 +369,6 @@ import java.util.UUID;
mDiscoveredPeripherals = new Hashtable<>();
}
this.mIsCentral = true;
this.mSession.setSelfAsCentral();
mScanHandler = new Handler();
......@@ -413,7 +414,7 @@ import java.util.UUID;
}
}
if (isFound) {
if (isFound && !mDiscoveredPeripherals.containsKey(device.getAddress())) {
Log.d(TAG, "onScanResult: new device found : " + device.getName() + " with rssi : " + result.getRssi());
NCMCPeripheralInfo info = new NCMCPeripheralInfo();
info.bluetoothGatt = null;
......@@ -422,6 +423,7 @@ import java.util.UUID;
info.writeWithResponseCharacteristic = null;
info.writeWithoutResponseCharacteristic = null;
info.mtu = DEFAULT_MTU;
info.device = device;
mDiscoveredPeripherals.put(device.getAddress(), info);
NCMCPeerID peerID = new NCMCPeerID(device.getName(), device.getAddress());
......@@ -429,8 +431,8 @@ import java.util.UUID;
mCentralService.notifyFoundPeer(peerID);
}
// stop scan
//m_isScanningOrAdvertising = false;
//m_LEScanner.stopScan(m_scanCallback);
mIsBrowsingOrAdvertising = false;
stopBrowsing();
//broadcastStatus(BLE_CONNECTION_AUTO_STOP_SCAN_ACTION);
// connect to peripheral
......@@ -673,12 +675,12 @@ import java.util.UUID;
private void connectToPeripheral(NCMCPeripheralInfo info) {
if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.M) {
info.bluetoothGatt = info.bluetoothGatt.getDevice().connectGatt(mContext, false, mGattCallback, BluetoothDevice.TRANSPORT_LE);
info.bluetoothGatt = info.device.connectGatt(mContext, false, mGattCallback, BluetoothDevice.TRANSPORT_LE);
} else {
try {
Method m = info.bluetoothGatt.getDevice().getClass().getDeclaredMethod("connectGatt", Context.class, boolean.class, BluetoothGattCallback.class, int.class);
int transport = info.bluetoothGatt.getDevice().getClass().getDeclaredField("TRANSPORT_LE").getInt(null); // LE = 2, BREDR = 1, AUTO = 0
info.bluetoothGatt = (BluetoothGatt) m.invoke(info.bluetoothGatt.getDevice(), mContext, false, mGattCallback, transport);
Method m = info.device.getClass().getDeclaredMethod("connectGatt", Context.class, boolean.class, BluetoothGattCallback.class, int.class);
int transport = info.device.getClass().getDeclaredField("TRANSPORT_LE").getInt(null); // LE = 2, BREDR = 1, AUTO = 0
info.bluetoothGatt = (BluetoothGatt) m.invoke(info.device, mContext, false, mGattCallback, transport);
} catch (Exception ex) {
ex.printStackTrace();
}
......
package com.nclab.ncmultipeerconnectivity;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
......@@ -13,4 +14,5 @@ class NCMCPeripheralInfo {
public BluetoothGattCharacteristic writeWithoutResponseCharacteristic;
public String name;
public int mtu;
public BluetoothDevice device;
}
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