Welcome Guest,Register Now
Log In

ANT Forum

Welcome guest, please Login or Register

   

Pairing ant+ HR programmatically

Rank

Total Posts: 5

Joined 2013-12-13

PM

Ive been reading this post ove rand over again, and as i understand there is to be a way to pair phone with ant+ device programmatically.

Now, ive tested everything... asyncscan, request access and so on... everything ive tested this far alway pop up plugin pairing UI. Not my project needs to do everything via code since the user never has access to any screens. That mean there will never be any user interactions.

So basically i need to in my app, constantly seek for nearby devices... if there are one found, i need to pair it and then connect to it to get data in the app.

Have i misunderstood something or is it possible ?

Might be useful... heres my code, i call the searchForAnts to seek for new devices:

public void searchForANTS(){
     Log
.d(TAG"1 Search for ants..");

          
/*AntPlusHeartRatePcc.requestAccess(defaultActivity,defaultActivity.getBaseContext(), pluginAccess, deviceStateChange);*/
        
     
hrScanCtrl AntPlusHeartRatePcc.requestAsyncScanController(this.defaultActivity10
                new 
IAsyncScanResultReceiver()
            
{
                
@Override
                
public void onSearchStopped(RequestAccessResult reasonStopped)
                
{
                    
//The triggers calling this function use the same codes and require the same actions as those received by the standard access result receiver
                    //base_IPluginAccessResultReceiver.onResultReceived(null, reasonStopped, DeviceState.DEAD);
                 
Log.d(TAG"ant+ device.dead");
                
}
                
                
@Override
                
public void onSearchResult(final AsyncScanResultDeviceInfo deviceFound)
                
{
                    
for(AsyncScanResultDeviceInfo imScannedDeviceInfos)
                    
{            
                        
//The current implementation of the async scan will reset it's ignore list every 30s,
                        //So we have to handle checking for duplicates in our list if we run longer than that
                        
if(i.getAntDeviceNumber() == deviceFound.getAntDeviceNumber())
                        
{
                            
//Found already connected device, ignore
                         
Log.d(TAG"ant+ found connected device");
                         
requestConnectToResult(deviceFound);

                         return;
                        
}
                    }

                    
//We split up devices already connected to the plugin from un-connected devices to make this information more visible to the user,
                    //since the user most likely wants to be aware of which device they are already using in another app
                    
if(deviceFound.isAlreadyConnected())
                    
{
                        mAlreadyConnectedDeviceInfos
.add(deviceFound);
                        
                                
//adapter_connDevNameList.add(deviceFound.getDeviceDisplayName());
                                //adapter_connDevNameList.notifyDataSetChanged();
                                
Log.d(TAG"ant+ already connected " deviceFound.getDeviceDisplayName());

                                
                    
}
                    
else
                    
{
                        mScannedDeviceInfos
.add(deviceFound);
                        
                               
// adapter_devNameList.add(deviceFound.getDeviceDisplayName());
                                //adapter_devNameList.notifyDataSetChanged();
                                
Log.d(TAG"ant+ unpaired device found " deviceFound.getDeviceDisplayName());

                                
requestConnectToResult(deviceFound);

                                
                                
                    
}
                }
            }
);
            
     
     
Log.d(TAG"ANT+ passed here");
    
}
    
protected void requestConnectToResult(final AsyncScanResultDeviceInfo asyncScanResultDeviceInfo)
    
{
        Log
.d(TAG"Connecting to " asyncScanResultDeviceInfo.getDeviceDisplayName());

        
hrScanCtrl.requestDeviceAccess(asyncScanResultDeviceInfo
            new 
IPluginAccessResultReceiver<AntPlusHeartRatePcc>()
            
{
            
@Override
            
public void onResultReceived(AntPlusHeartRatePcc result,
                
RequestAccessResult resultCodeDeviceState initialDeviceState)
            
{
                
if(resultCode == RequestAccessResult.SEARCH_TIMEOUT)
                
{
                    
//On a connection timeout the scan automatically resumes, so we inform the user, and go back to scanning
                    
Log.d(TAG"Scanning for heart rate devices asynchronously...");
                    
                
}
                
else
                
{
                    
//Otherwise the results, including SUCCESS, behave the same as 
                    
pluginAccess.onResultReceived(resultresultCodeinitialDeviceState);
                
}
            }
            }
deviceStateChange);
    
     
Rank

Total Posts: 5

Joined 2013-12-13

PM

Got the solution, didnt need to pair... works well with unpaired device, so i added requestConnectToResult in below if-case

if(deviceFound.isAlreadyConnected())
{
mAlreadyConnectedDeviceInfos.add(deviceFound);

//adapter_connDevNameList.add(deviceFound.getDeviceDisplayName());
//adapter_connDevNameList.notifyDataSetChanged();
Log.d(TAG, "ant+ already connected " + deviceFound.getDeviceDisplayName());

requestConnectToResult(deviceFound); // This one... connects to device and gives me data smile

}