简体   繁体   中英

Starting application in another Android smartphone over bluetooth

As the title suggests, I am developing an app, which can connect and pair with another Android phone. I can successfully scan, connect, pair and transmit String data over bluetooth. I tested by sending String data and received it in another android smartphone and showed it in the toast.

The actual scenario

This application will be installed in both android phones. Once another android phone scans and connect to the phone over bluetooth, then I want to launch this app. I know we can accomplish this using BroadcastReceiver. But how can I implement this in terms of Bluetooth connection. Any code snippet or idea or suggestions are welcomed..

After you get the BT msg,Hope you followed this link ,Here after "// Send the obtained bytes to the UI activity" Send a broadcast to start your app.

Loop with the list of apps installed and know which app to be launched and use the below to launch a app.

public String getPreInstalledPocPkgName(){
        String pkgName = null;
        try {
            List<PackageInfo> pkgInfoList = mContext.getPackageManager().getInstalledPackages(0);

            for(PackageInfo pkgInfo:pkgInfoList){

                pkgName = pkgInfo.packageName;

                if(pkgName==null || pkgName.equals("")){
                    continue;
                }
                for(String name:pttPkgList){
                    if(pkgName.equals(name)){
                        Log.d(TAG, "------------ app with package name ["+pkgName+"] already installed----------");
                        return pkgName;
                    }
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "-------Error! while fetching previously installed  app------", e);
            clearConfigFromSharedPath();
        }
        Log.d(TAG, "------------No previously installed  app----------");
        return null;
    } 
}



   String pttAppPkgName = getPreInstalledPocPkgName();
       Or 
         pttAppPkgName = your app package.
        PackageManager pm = getPackageManager();
        Intent intent = pm.getLaunchIntentForPackage(pttAppPkgName);
        startActivity(intent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM