@@ -32,14 +32,14 @@ public class BluetoothConnection : IConnectionContext
32
32
private BluetoothGattCharacteristicConnection m_pairingGDIO = null ;
33
33
private BluetoothGattCharacteristicConnection m_GDIO = null ;
34
34
private BluetoothGattCharacteristicConnection m_UGDIO = null ;
35
- public string DeviceID { get ; private set ; }
35
+ public string DeviceName => m_connectionInfo . DeviceName ;
36
36
public static Collection Connections => Collection . Instance ;
37
- private BluetoothConnectionInfo connectionInfo = new BluetoothConnectionInfo ( ) ;
38
- public ClientPublicKey ClientPublicKey => connectionInfo . ClientPublicKey ;
39
- public SmartLockPublicKey SmartLockPublicKey => connectionInfo . SmartLockPublicKey ;
40
- public SharedKey SharedKey => connectionInfo . SharedKey ;
41
- public UniqueClientID UniqueClientID => connectionInfo . UniqueClientID ;
42
- public SmartLockUUID SmartLockUUID => connectionInfo . SmartLockUUID ;
37
+ private BluetoothConnectionInfo m_connectionInfo = new BluetoothConnectionInfo ( ) ;
38
+ public ClientPublicKey ClientPublicKey => m_connectionInfo . ClientPublicKey ;
39
+ public SmartLockPublicKey SmartLockPublicKey => m_connectionInfo . SmartLockPublicKey ;
40
+ public SharedKey SharedKey => m_connectionInfo . SharedKey ;
41
+ public UniqueClientID UniqueClientID => m_connectionInfo . UniqueClientID ;
42
+ public SmartLockUUID SmartLockUUID => m_connectionInfo . SmartLockUUID ;
43
43
44
44
public SmartLockNonce SmartLockNonce
45
45
{
@@ -66,36 +66,37 @@ private string RemoveServiceID(string strDeviceID)
66
66
var matches = regex . Matches ( strDeviceID ) ;
67
67
return matches [ matches . Count - 1 ] ? . Groups [ "GUID" ] ? . Value ?? strDeviceID ;
68
68
}
69
- public BluetoothConnection this [ string strDeviceID ]
69
+ public BluetoothConnection this [ string strDeviceName ]
70
70
{
71
71
get
72
72
{
73
73
BluetoothConnection connection = null ;
74
- string strUniqueID = RemoveServiceID ( strDeviceID ) ;
75
- if ( ! s_Connections . TryGetValue ( strUniqueID , out connection ) )
74
+ if ( ! s_Connections . TryGetValue ( strDeviceName , out connection ) )
76
75
{
77
- s_Connections . TryAdd ( strUniqueID , new BluetoothConnection ( strUniqueID ) ) ;
78
- s_Connections . TryGetValue ( strUniqueID , out connection ) ;
76
+ s_Connections . TryAdd ( strDeviceName , new BluetoothConnection ( strDeviceName ) ) ;
77
+ s_Connections . TryGetValue ( strDeviceName , out connection ) ;
79
78
}
80
79
else { }
81
80
return connection ;
82
81
}
83
82
}
84
83
}
85
84
86
- private BluetoothConnection ( string strUniqeDeviceID )
85
+ private BluetoothConnection ( string strDeviceName )
87
86
{
88
- DeviceID = strUniqeDeviceID ;
89
- connectionInfo . UniqueClientID = new UniqueClientID ( 5 ) ;
87
+ m_connectionInfo . DeviceName = strDeviceName ;
88
+ m_connectionInfo . UniqueClientID = new UniqueClientID ( 5 ) ;
90
89
m_pairingGDIO = new BluetoothGattCharacteristicConnection ( ) ;
91
90
m_GDIO = new BluetoothGattCharacteristicConnection ( ) ;
92
91
m_UGDIO = new BluetoothGattCharacteristicConnection ( ) ;
93
92
}
94
- public async Task < bool > Connect ( string strDeviceID )
93
+ public async Task < bool > Connect ( string strDeviceID , BluetoothConnectionInfo connectionInfo = null )
95
94
{
96
95
bool blnRet = false ;
97
96
try
98
97
{
98
+ if ( connectionInfo != null )
99
+ m_connectionInfo = connectionInfo ;
99
100
var deviceService = await GattDeviceService . FromIdAsync ( strDeviceID ) ;
100
101
if ( deviceService != null )
101
102
{
@@ -104,15 +105,14 @@ public async Task<bool> Connect(string strDeviceID)
104
105
if ( character . Uuid == KeyTurnerGDIO . Value )
105
106
{
106
107
m_GDIO . SetConnection ( character ) ;
107
- connectionInfo . DeviceID = strDeviceID ;
108
108
}
109
109
else if ( character . Uuid == KeyTurnerPairingGDIO . Value )
110
110
{
111
111
m_pairingGDIO . SetConnection ( character ) ;
112
112
}
113
113
else if ( character . Uuid == KeyTurnerUGDIO . Value )
114
114
{
115
- connectionInfo . DeviceID = strDeviceID ;
115
+
116
116
m_UGDIO . SetConnection ( character ) ;
117
117
}
118
118
}
@@ -134,7 +134,7 @@ public async Task<bool> Connect(string strDeviceID)
134
134
135
135
136
136
137
- public async Task < BluetoothPairResult > PairDevice ( )
137
+ public async Task < BluetoothPairResult > PairDevice ( string strConnectionName )
138
138
{
139
139
BlutoothPairStatus status = BlutoothPairStatus . Failed ;
140
140
try
@@ -146,7 +146,8 @@ public async Task<BluetoothPairResult> PairDevice()
146
146
147
147
SendBaseCommand cmd = new SendRequestDataCommand ( CommandTypes . PublicKey ) ;
148
148
Sodium . KeyPair keyPair = Sodium . PublicKeyBox . GenerateKeyPair ( ) ;
149
- connectionInfo . ClientPublicKey = new ClientPublicKey ( keyPair . Public ) ;
149
+ m_connectionInfo . ClientPublicKey = new ClientPublicKey ( keyPair . Public ) ;
150
+ m_connectionInfo . ConnectionName = strConnectionName ;
150
151
if ( await m_pairingGDIO . Send ( cmd ) ) //3.
151
152
{
152
153
var response = await m_pairingGDIO . Recieve ( 2000 ) ; //4.
@@ -160,12 +161,12 @@ public async Task<BluetoothPairResult> PairDevice()
160
161
161
162
if ( await m_pairingGDIO . Send ( cmd ) ) //6.
162
163
{
163
- connectionInfo . SmartLockPublicKey = ( ( RecievePublicKeyCommand ) response ) . PublicKey ;
164
+ m_connectionInfo . SmartLockPublicKey = ( ( RecievePublicKeyCommand ) response ) . PublicKey ;
164
165
165
166
byte [ ] byDH1 = Sodium . ScalarMult . Mult ( keyPair . Secret , SmartLockPublicKey ) ;
166
167
var _0 = new byte [ 16 ] ;
167
168
var sigma = System . Text . Encoding . UTF8 . GetBytes ( "expand 32-byte k" ) ;
168
- connectionInfo . SharedKey = new SharedKey ( Sodium . KDF . HSalsa20 ( _0 , byDH1 , sigma ) ) ; //8
169
+ m_connectionInfo . SharedKey = new SharedKey ( Sodium . KDF . HSalsa20 ( _0 , byDH1 , sigma ) ) ; //8
169
170
170
171
response = await m_pairingGDIO . Recieve ( 2000 ) ;
171
172
@@ -193,8 +194,8 @@ public async Task<BluetoothPairResult> PairDevice()
193
194
194
195
if ( response ? . CommandType == CommandTypes . AuthorizationID ) //19
195
196
{
196
- connectionInfo . UniqueClientID = ( ( RecieveAuthorizationIDCommand ) response ) . UniqueClientID ;
197
- connectionInfo . SmartLockUUID = ( ( RecieveAuthorizationIDCommand ) response ) . SmartLockUUID ;
197
+ m_connectionInfo . UniqueClientID = ( ( RecieveAuthorizationIDCommand ) response ) . UniqueClientID ;
198
+ m_connectionInfo . SmartLockUUID = ( ( RecieveAuthorizationIDCommand ) response ) . SmartLockUUID ;
198
199
this . SmartLockNonce = ( ( RecieveAuthorizationIDCommand ) response ) . SmartLockNonce ;
199
200
cmd = new SendAuthorizationIDConfirmationCommand ( UniqueClientID , this ) ;
200
201
@@ -289,7 +290,7 @@ public async Task<BluetoothPairResult> PairDevice()
289
290
Debug . WriteLine ( "Error in pairing: {0}" , ex ) ;
290
291
status = BlutoothPairStatus . Failed ;
291
292
}
292
- return new BluetoothPairResult ( status , ( status == BlutoothPairStatus . Successfull ) ? connectionInfo : null ) ;
293
+ return new BluetoothPairResult ( status , ( status == BlutoothPairStatus . Successfull ) ? m_connectionInfo : null ) ;
293
294
}
294
295
295
296
public ClientNonce CreateNonce ( )
0 commit comments