Read the original article:Unable to obtain NearLink connection status
Problem Description
After compressing data with zip.deflate and calling zStream.totalOut, the totalOut value of zStream remains unchanged at 0 even after encoding. How can I obtain the length of the encoded data?
Background Knowledge
The StarFlash SSAP module provides the on('connectionStateChange') interface for subscribing to StarFlash connection state change events, and also offers the corresponding off('connectionStateChange') for unsubscribing from connection state change events.
The StarFlash remoteDevice module provides the getConnectionState interface to actively retrieve the connection status between the local device and the remote device.
Troubleshooting Process
Investigate the business logic code to verify whether the off('connectionStateChange') interface is invoked when disconnecting the StarFlash connection.
Anaysis Conclusion
When disconnecting the StarFlash connection, calling the off('connectionStateChange') interface cancels the subscription to connection state change events, preventing the on('connectionStateChange') interface callback from returning the StarFlash connection status.
Solution
Option 1: When disconnecting from StarFlash, there is no need to immediately call the off('connectionStateChange') interface. Instead, wait until the on('connectionStateChange') interface callback confirms that StarFlash has successfully disconnected, then call off('connectionStateChange'). Reference code is as follows:
import { constant, ssap } from '@kit.NearLinkKit';
@Entry
@Component
struct Index {
@State client: ssap.Client | undefined = undefined;
// Scan the obtained remote device address. @State addr: string = 'XX:XX:XX:XX:XX:XX';
// Subscription connection listening.
onConnectionStateChange() {
this.client?.on('connectionStateChange', (data: ssap.ConnectionChangeState) => {
// SparkLink has been successfully connected.
if (data.state === constant.ConnectionState.STATE_CONNECTED) {
console.info('connect success:' + data.state);
}
// The Starlink connection was detected as disconnected, and the off('connectionStateChange') interface was called to unsubscribe.
if (data.state === constant.ConnectionState.STATE_DISCONNECTED) {
this.client?.off('connectionStateChange');
}
});
}
// Begin connecting.
connect() {
this.client?.connect();
}
// Disconnect.
disConnect() {
this.client?.disconnect().then(() => {
console.info('disconnect success');
});
}
build() {
Column() {
Button('connect').onClick(() => {
this.connect()
})
Button('disConnect').onClick(() => {
this.disConnect()
})
}
}
}
Option 2: After disconnecting, you can proactively retrieve the current StarFlash connection status via the getConnectionState interface of the StarFlash remoteDevice module. The usage example for the getConnectionState interface is as follows:
import { abilityAccessCtrl, common, PermissionRequestResult } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { remoteDevice } from '@kit.NearLinkKit';
@Entry
@Component
struct getConnectionState {
// Scan the obtained remote device address. When using it, you need to modify it according to the actual situation.
@State addr: string = 'XX:XX:XX:XX:XX:XX';
uiContext = this.getUIContext();
aboutToAppear(): void {
let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
atManager.requestPermissionsFromUser(this.uiContext?.getHostContext() as common.UIAbilityContext,
['ohos.permission.ACCESS_NEARLINK'], (err: BusinessError, data: PermissionRequestResult) => {
if (err) {
console.error(`requestPermissionsFromUser fail, err->${JSON.stringify(err)}`);
} else {
console.info(`data:${JSON.stringify(data)}`);
}
})
}
getConnectionState() {
// Create a RemoteDevice instance object.
let device = remoteDevice.createRemoteDevice(this.addr);
// Obtain the connection status between the local device and the remote device.
let state: remoteDevice.ConnectionState = device.getConnectionState();
console.info('state:' + state);
}
build() {
Column() {
Button() {
Text('getConnectionState')
}
.onClick(() => {
this.getConnectionState();
})
}
}
}
Note: The ACCESS_NEARLINK permission should be added to the requestPermissions section in module.json5.
{
"name": "ohos.permission.ACCESS_NEARLINK",
"reason": "$string:EntryAbility_desc",
"usedScene": {
"abilities": [
"EntryAbility"
],
"when": "always"
}
}

Top comments (0)