首页 文章 精选 留言 我的

精选列表

搜索[visual studio code],共10000篇文章
优秀的个人博客,低调大师

Android Studio 解决Error:(781) Multiple substitutions specified in non-...

异常信息: /Users/mazaiting/AndroidStudioProjects/WisdomSite/app/build/intermediates/res/merged/debug/values/values.xml Error:(781) Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute? Error:(781) Unexpected end tag string Error:(781) Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute? Error:(781) Unexpected end tag string Error:Execution failed for task ':app:processDebugResources'. > com.android.ide.common.process.ProcessException: Failed to execute aapt 异常.png 解决办法: 将 <string name="tv_attend_day">\t\t应到 %d人,实到 %d人,到勤率 %.2f%%,缺勤率 %.2f%%.</string> 更改为: <string name="tv_attend_day">\t\t应到 %1$d人,实到 %2$d人,到勤率 %3$.2f%%,缺勤率 %4$.2f%%.</string>

优秀的个人博客,低调大师

一个Android studio的plugins的安装异常

正常情况下,我们都是进入setting到plugins中,点击Browse进行搜素就好了 本图来自https://www.cnblogs.com/whycxb/p/6850454.html 本图来自https://www.cnblogs.com/whycxb/p/6850454.html 今天下载一个插件时,总是超时(timed out),后来索性直接到错误提示的网站上直接下载到本地, 不过下载完了怎么装上as上面,是个问题, 后来发现了有这么个按钮(install plugin form disk...) 点击之后会弹出本地目录,选择刚刚强制上网站下载的plugin包,直接ok就可以安装了!

优秀的个人博客,低调大师

Android Studio Emulator 提示 “/dev/kvm is not found” 解决办法

重新安装HAXM即可解决 1.确定已经安装HAXMSDK Manager -> Extras -> Intel x86 Emulator Accelerator (HAXM installer) 2.进入HAXM目录<sdk>/extras/intel/Hardware_Accelerated_Execution_Manager/ 3.运行卸载HAXM命令./HAXM installation -u 4.重新安装./HAXM installation 本文转自 一点点征服 博客园博客,原文链接:http://www.cnblogs.com/ldq2016/p/7987085.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

在Android studio模拟器中运行apk文件

菜鸟级玩家比看文。 win平台下下载的apk文件,怎么能模拟运行出来。 首先得安装Android SDK,不会的自行百度一下。 接下来,打开AVD模拟器,自己创建一个模拟器(过程自己实践) 然后,将你的APK文件COPY到下面所示文件夹下: 最后,win+r打开终端,切换到上面的platform-tools文件夹里面,运行:adb install demo.apk,出现success就添加成功了。

优秀的个人博客,低调大师

android studio 中出现"...ProjectScript\buildscript\cache.properties.lock"

OwnerPID:unknown OurPID:8496 OwnerOperation:unknown Ouroperation:Initializecache Lockfile:C:\Users\Administrator\.gradle\caches\2.2.1\scripts\build_81ep6udn3nlzszbotl32uedjz\ProjectScript\buildscript\cache.properties.lock 找到红色标识部分文件然后删除文件。删除的时候会提示文件被占用。开打进程管理器把关于 java的进程结束后,在Android Stuido中重新build就可以。 本文转自 netcorner 博客园博客,原文链接: http://www.cnblogs.com/netcorner/p/4627269.html ,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android Studio2.2.3 通过JNI引用ffmpeg库小结

修改步骤: 首先通过NDK14编译出libffmpeg.so ,将include目录取出 通过AS建立基于jni的工程项目,将include目录放到cpp下;创建jniLibs/armeabi目录,将libffmpeg.so放到里边 3.配置CMakeLists.txt 添加如下: #addtheffmpeglib include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp/include) add_library(ffmpeg-libSHAREDIMPORTED) set_target_properties(ffmpeg-libPROPERTIESIMPORTED_LOCATION${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/armeabi/libffmpeg.so) target_link_libraries(native-lib ffmpeg-lib#ffmpeglib android#usetheandroidloglib ${log-lib}) 4.配置build.gradle 主要添加arguments 和 ndk的配置 externalNativeBuild{ cmake{ cppFlags"" arguments'-DANDROID_TOOLCHAIN=clang','-DANDROID_STL=gnustl_static' } ndk{ abiFilters'armeabi' } } 5.native_lib.cpp #include <jni.h> #include <string> #include <android/log.h> extern "C" { #include "libavcodec/avcodec.h" #include "libavformat/avformat.h" #include "libswscale/swscale.h" #include "libavutil/imgutils.h" #include "libavutil/avutil.h" } #define LOG_TAG "MyTagFFmpeg" #define ALOGE(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) extern "C" jstring Java_letv_com_myffmpeg_MainActivityFFmpeg_stringFromJNI( JNIEnv *env, jobject /* this */) { std::string hello = "Hello ,this is the first ffmpeg test !"; ALOGE("%s",hello.c_str()); char info[10000] = { 0 }; sprintf(info, "%s\n", avcodec_configuration()); hello += info; return env->NewStringUTF(hello.c_str()); } That's all. 本文转自 曾永刚 51CTO博客,原文链接:http://blog.51cto.com/zyg0227/1947097

优秀的个人博客,低调大师

Android Studio - 第四十五期 Gradle manifestPlaceholders

最近在学习撸撸的代码规范和写法,有些心得,准备好好写一写~包括了多渠道打版(以前有写过方法),工厂模式,mvp,以及最近刚封装出来的多渠道多版本展示不同页面的manifestPlaceholders的配置方法,大家应该也碰到过线上和线下环境的切换换地址,换私钥的头大问题,本篇就来解决这些问题。 先在androidmanifest文件配置一个节点,这里以极光为例: 1 2 3 4 5 6 7 <meta-data android:name= "JPUSH_APPKEY" android:value= "${jush_appkey_value}" /> <meta-data android:name= "SHOUCANG_CONFIG0" android:value= "${SHOUCANG_CONFIG_VALUE0}" /> build.gradle: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 buildTypes{ release{ //自定义buildconfig字段 buildConfigField( "boolean" , "APP_ENV" , "true" ) //指定签名为release signingConfigsigningConfig.release //是否开启混淆 minifyEnabled false proguardFilesgetDefaultProguardFile( 'proguard-android.txt' ), 'proguard-rules.pro' //是否zip优化 zipAlignEnabled true //删除一些无用资源 shrinkResources false // manifestPlaceholders=[ "jush_appkey_value" : "releasekey" ] } debug{ //自定义buildconfig字段 buildConfigField( "boolean" , "APP_ENV" , "true" ) //指定签名为release signingConfigsigningConfig.release //是否开启混淆 minifyEnabled false proguardFilesgetDefaultProguardFile( 'proguard-android.txt' ), 'proguard-rules.pro' //是否zip优化 zipAlignEnabled true //删除一些无用资源 shrinkResources false // manifestPlaceholders=[ "jush_appkey_value" : "debugkey" ] } } 在bulidtypes节点下有release节点和debug节点,正式签名时就会走release节点的下编译脚本,调试签名时就会走debug节点。主要点就是manifestPlaceholders的用法,jpush_appkey对应的就是之前在androidmanifest文件配置的${jush_appkey_value}的这个值。 在程序入口出打上log,用来输出key的值: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /** *在程序入口出打上log,用来输出key的值bufen */ private void jpush_key_manifest_xml_string(){ Stringjpush_appkey; try { ApplicationInfoappInfo=getPackageManager() .getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); jpush_appkey=appInfo.metaData.getString( "JPUSH_APPKEY" ); Log.e( "jpush_appkey" , "jpush_appkey=" +jpush_appkey); } catch (PackageManager.NameNotFoundExceptione){ e.printStackTrace(); } } 接下来给大家介绍多版本多页面多apk的配置切换方法:举个例子:如果你要一次性打七个版本,而且七个版本都是不同的页面,但是页面各个模块大体一样,只是顺序和大小不同,你要怎么做,别告诉我写七个页面分别打版哈~太low了~you know~这里就利用多版本打版和manifestPlaceholders来实现需求。 首先是build.gradle: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 applyplugin: 'com.android.application' applyplugin: 'android-apt' defdemo= '0000' ; //DemoAPK defdemo1= '0001' ; //DemoAPK1 defdemo2= '0002' ; //DemoAPK2 defdemo3= '0003' ; //DemoAPK3 defdemo4= '0004' ; //DemoAPK4 defdemo5= '0005' ; //DemoAPK5 defdemo6= '0006' ; //DemoAPK6 android{ signingConfigs{ debug{ keyAlias '****' keyPassword '****' storeFilefile( '签名文件.jks路径' ) storePassword '****' } release{ keyAlias '****' keyPassword '****' storeFilefile( '签名文件.jks路径' ) storePassword '****' } } compileSdkVersion 25 buildToolsVersion "25.0.2" sourceSets{ main{ jniLibs.srcDirs=[ 'libs' ] } } packagingOptions{ exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' } defaultConfig{ applicationId "com.shining.p010_recycleviewall" minSdkVersion 18 targetSdkVersion 23 versionCode 1 versionName "1.0" multiDexEnabled true renderscriptTargetApi 19 renderscriptSupportModeEnabled true ndk{ moduleName "native-modbus-jni,libxmediaplayer" ldLibs "log" , "z" , "m" , "android" , "c" abiFilters "armeabi" , "armeabi-v7a" , "x86" } sourceSets.main{ jni.srcDirs=[] //LOCAL_LDFLAGS+=-fuse-ld=bfd //jni.srcDirs'src/main/jni' jniLibs.srcDir 'src/main/libs' } signingConfigsigningConfigs.debug manifestPlaceholders=[ SHOUCANG_CONFIG_VALUE0: ".shoucang.factorys.ShoucangFactory0" ] } buildTypes{ release{ minifyEnabled true zipAlignEnabled true shrinkResources false proguardFilesgetDefaultProguardFile( 'proguard-android.txt' ), 'proguard-rules.pro' signingConfigsigningConfigs.debug } } def int minSdk= 18 ; def int targetSdk= 23 ; defStringappId= 'com.shining.p010_recycleviewall' ; def int vCode= 1 ; defStringvNameCode=vCode+ "" ; productFlavors{ //demo DemoAPK{ minSdkVersionminSdk applicationIdappId targetSdkVersiontargetSdk versionCodevCode versionName "DemoAPK_" + "T_" +demo multiDexEnabled true renderscriptTargetApi 19 renderscriptSupportModeEnabled true ndk{ moduleName "native-modbus-jni,libxmediaplayer" ldLibs "log" , "z" , "m" , "android" , "c" abiFilters "armeabi" , "armeabi-v7a" , "x86" } sourceSets.main{ jni.srcDirs=[] jniLibs.srcDir 'src/main/libs' } signingConfigsigningConfigs.debug } //demo1 DemoAPK1{ minSdkVersionminSdk applicationIdappId targetSdkVersiontargetSdk versionCodevCode versionName "DemoAPK1_" + "T_" +demo1 multiDexEnabled true renderscriptTargetApi 19 renderscriptSupportModeEnabled true ndk{ moduleName "native-modbus-jni,libxmediaplayer" ldLibs "log" , "z" , "m" , "android" , "c" abiFilters "armeabi" , "armeabi-v7a" , "x86" } sourceSets.main{ jni.srcDirs=[] jniLibs.srcDir 'src/main/libs' } signingConfigsigningConfigs.debug } //demo2 DemoAPK2{ minSdkVersionminSdk applicationIdappId targetSdkVersiontargetSdk versionCodevCode versionName "DemoAPK2_" + "T_" +demo2 multiDexEnabled true renderscriptTargetApi 19 renderscriptSupportModeEnabled true ndk{ moduleName "native-modbus-jni,libxmediaplayer" ldLibs "log" , "z" , "m" , "android" , "c" abiFilters "armeabi" , "armeabi-v7a" , "x86" } sourceSets.main{ jni.srcDirs=[] jniLibs.srcDir 'src/main/libs' } signingConfigsigningConfigs.debug } //demo3 DemoAPK3{ minSdkVersionminSdk applicationIdappId targetSdkVersiontargetSdk versionCodevCode versionName "DemoAPK3_" + "T_" +demo3 multiDexEnabled true renderscriptTargetApi 19 renderscriptSupportModeEnabled true ndk{ moduleName "native-modbus-jni,libxmediaplayer" ldLibs "log" , "z" , "m" , "android" , "c" abiFilters "armeabi" , "armeabi-v7a" , "x86" } sourceSets.main{ jni.srcDirs=[] jniLibs.srcDir 'src/main/libs' } signingConfigsigningConfigs.debug } //demo4 DemoAPK4{ minSdkVersionminSdk applicationIdappId targetSdkVersiontargetSdk versionCodevCode versionName "DemoAPK4_" + "T_" +demo4 multiDexEnabled true renderscriptTargetApi 19 renderscriptSupportModeEnabled true ndk{ moduleName "native-modbus-jni,libxmediaplayer" ldLibs "log" , "z" , "m" , "android" , "c" abiFilters "armeabi" , "armeabi-v7a" , "x86" } sourceSets.main{ jni.srcDirs=[] jniLibs.srcDir 'src/main/libs' } signingConfigsigningConfigs.debug } //demo5 DemoAPK5{ minSdkVersionminSdk applicationIdappId targetSdkVersiontargetSdk versionCodevCode versionName "DemoAPK5_" + "T_" +demo5 multiDexEnabled true renderscriptTargetApi 19 renderscriptSupportModeEnabled true ndk{ moduleName "native-modbus-jni,libxmediaplayer" ldLibs "log" , "z" , "m" , "android" , "c" abiFilters "armeabi" , "armeabi-v7a" , "x86" } sourceSets.main{ jni.srcDirs=[] jniLibs.srcDir 'src/main/libs' } signingConfigsigningConfigs.debug } //demo6 DemoAPK6{ minSdkVersionminSdk applicationIdappId targetSdkVersiontargetSdk versionCodevCode versionName "DemoAPK6_" + "D_" +demo6 multiDexEnabled true renderscriptTargetApi 19 renderscriptSupportModeEnabled true ndk{ moduleName "native-modbus-jni,libxmediaplayer" ldLibs "log" , "z" , "m" , "android" , "c" abiFilters "armeabi" , "armeabi-v7a" , "x86" } sourceSets.main{ jni.srcDirs=[] jniLibs.srcDir 'src/main/libs' } signingConfigsigningConfigs.debug } } //自定义输出配置 applicationVariants.all{variant-> variant.outputs.each{output-> defoutputFile=output.outputFile if (outputFile!= null &&outputFile.name.endsWith( '.apk' )){ //deffileName="UerbT_v${variant.versionName}_${releaseTime()}_${variant.flavorName}.apk" deffileName= "${variant.versionName}.apk" output.outputFile= new File(outputFile.parent,fileName) } } } productFlavors.all{flavor-> defcurrentMode=flavor.versionName.split( "_" )[ 2 ] defcurrentEnvironment=flavor.versionName.split( "_" )[ 1 ] defstValue= true //t==currentEnvironment以前的判断条件 if (currentEnvironment.endsWith( "T" )){ //判断是否为测试版是否以T结尾 stValue= false } else { stValue= true } if (currentMode==demo){ flavor.manifestPlaceholders=[SHOUCANG_CONFIG_VALUE: ".shoucang.factorys.ShoucangFactory" ,STATISTICS_VALUE:stValue] } else if (currentMode==demo1){ flavor.manifestPlaceholders=[SHOUCANG_CONFIG_VALUE: ".shoucang.factorys.ShoucangFactory1" ,STATISTICS_VALUE:stValue] } else if (currentMode==demo2){ flavor.manifestPlaceholders=[SHOUCANG_CONFIG_VALUE: ".shoucang.factorys.ShoucangFactory2" ,STATISTICS_VALUE:stValue] } else if (currentMode==demo3){ flavor.manifestPlaceholders=[SHOUCANG_CONFIG_VALUE: ".shoucang.factorys.ShoucangFactory3" ,STATISTICS_VALUE:stValue] } else if (currentMode==demo4){ flavor.manifestPlaceholders=[SHOUCANG_CONFIG_VALUE: ".shoucang.factorys.ShoucangFactory4" ,STATISTICS_VALUE:stValue] } else if (currentMode==demo5){ flavor.manifestPlaceholders=[SHOUCANG_CONFIG_VALUE: ".shoucang.factorys.ShoucangFactory5" ,STATISTICS_VALUE:stValue] } else if (currentMode==demo6){ flavor.manifestPlaceholders=[SHOUCANG_CONFIG_VALUE: ".shoucang.factorys.ShoucangFactory6" ,STATISTICS_VALUE:stValue] } } } dependencies{ compilefileTree(include:[ '*.jar' ],dir: 'libs' ) androidTestCompile( 'com.android.support.test.espresso:espresso-core:2.2.2' ,{ excludegroup: 'com.android.support' ,module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.0' compile 'com.android.support:recyclerview-v7:25.3.0' compile 'com.android.support:design:25.3.0' compile 'com.android.support:cardview-v7:25.3.0' //localjarfile compilefiles( 'libs/alipay-sdk-java20161226110022.jar' ) compilefiles( 'libs/alipay-sdk-java20161226110022-source.jar' ) compilefiles( 'libs/commons-logging-1.1.1.jar' ) compilefiles( 'libs/commons-logging-1.1.1-sources.jar' ) //thethirdfile compile 'com.jakewharton:butterknife:8.2.1' apt 'com.jakewharton:butterknife-compiler:8.2.1' testCompile 'junit:junit:4.12' compile 'com.geeklx:library_hios:1.0.4' compileproject( ':glin' ) compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.alibaba:fastjson:1.2.17' compile 'com.squareup.okio:okio:1.9.0' compile 'com.squareup.okhttp3:okhttp:3.4.1' compile 'com.nineoldandroids:library:2.4.0' compilefiles( 'libs/libammsdk.jar' ) } 接着就是多版本的代码判断书写: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 @Override protected void onCreate( @Nullable BundlesavedInstanceState){ //TODO多版本切换写此方法bufen which_version(); //ShoucangConfig0.config();//manifestPlaceholders的妙用 super .onCreate(savedInstanceState); } private void which_version(){ if (ConstantNetUtil.VERSION_APK==NetConfig.version_name0){ ShoucangConfig.config(); } else if (ConstantNetUtil.VERSION_APK==NetConfig.version_name1){ ShoucangConfig1.config(); } else if (ConstantNetUtil.VERSION_APK==NetConfig.version_name2){ ShoucangConfig2.config(); } else if (ConstantNetUtil.VERSION_APK==NetConfig.version_name3){ ShoucangConfig3.config(); } else if (ConstantNetUtil.VERSION_APK==NetConfig.version_name4){ ShoucangConfig4.config(); } else if (ConstantNetUtil.VERSION_APK==NetConfig.version_name5){ ShoucangConfig5.config(); } else if (ConstantNetUtil.VERSION_APK==NetConfig.version_name6){ ShoucangConfig6.config(); } } //TODO多版本模式bufen SparseArrayCompat<Class<? extends BaseFragment>>array=which_version_fragment_config(); //demo private SparseArrayCompat<Class<? extends BaseFragment>>which_version_fragment_config(){ if (ConstantNetUtil.VERSION_APK==NetConfig.version_name0){ return ShoucangConfig.getFragments(); } else if (ConstantNetUtil.VERSION_APK==NetConfig.version_name1){ return ShoucangConfig1.getFragments(); } else if (ConstantNetUtil.VERSION_APK==NetConfig.version_name2){ return ShoucangConfig2.getFragments(); } else if (ConstantNetUtil.VERSION_APK==NetConfig.version_name3){ return ShoucangConfig3.getFragments(); } else if (ConstantNetUtil.VERSION_APK==NetConfig.version_name4){ return ShoucangConfig4.getFragments(); } else if (ConstantNetUtil.VERSION_APK==NetConfig.version_name5){ return ShoucangConfig5.getFragments(); } else if (ConstantNetUtil.VERSION_APK==NetConfig.version_name6){ return ShoucangConfig6.getFragments(); } return ShoucangConfig.getFragments(); } 这样跑完apk你会发现会有神奇的事情发生,如下图:(不同的apk版本出来的页面也是不同的,但是只用了一份代码。) 图1: 图2: 这样做的好处在于,如果你的apk版本很多,需要给很多合作厂商提供定制化页面,就可以用上了~ 卧槽,今天喷了好多,希望大家回去自己细化一下,能帮到你~ 本文转自 吴雨声 51CTO博客,原文链接:http://blog.51cto.com/liangxiao/1951381,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android Studio第三十三期 - 蓝牙开发初识~

遇见到坑爹的队友只有接受现实并且解决问题~ 首先介绍一下网上几乎所有的能搜到的方法: 1.首先,要操作蓝牙,先要在AndroidManifest.xml里加入权限 1 2 <uses-permissionandroid:name= "android.permission.BLUETOOTH_ADMIN" /> <uses-permissionandroid:name= "android.permission.BLUETOOTH" /> 2.在android.bluetooth包下就这些能用的东西: 3.我们一般用的是BluetoothAdapter基本就可以实现需求了: cancelDiscovery() :取消发现,也就是说当我们正在搜索设备的时候调用这个方法将不再继续搜索 disable():关闭蓝牙 enable():打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候我们需要问下用户是否打开,一下这两行代码同样是打开蓝牙,不过会提示用户: 1 2 3 4 5 6 7 8 9 //打开蓝牙 if (!mBluetoothAdapter.isEnabled()){ Intentintent1= new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent1,ENABLE_BLUE); //不做提示,强行打开 //mAdapter.enable(); } else { close_blue(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Override protected void onActivityResult( int requestCode, int resultCode,Intentdata){ //TODOAuto-generatedmethodstub super .onActivityResult(requestCode,resultCode,data); if (requestCode==ENABLE_BLUE){ if (resultCode==RESULT_OK){ Toast.makeText( this , "蓝牙开启成功" ,Toast.LENGTH_SHORT).show(); open_blue(); } else if (resultCode==RESULT_CANCELED){ Toast.makeText( this , "蓝牙开始失败" ,Toast.LENGTH_SHORT).show(); close_blue(); } } } getAddress():获取本地蓝牙地址 getDefaultAdapter():获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdapter getName():获取本地蓝牙名称 getRemoteDevice(String address):根据蓝牙地址获取远程蓝牙设备 getState():获取本地蓝牙适配器当前状态(感觉可能调试的时候更需要) isDiscovering():判断当前是否正在查找设备,是返回true isEnabled():判断蓝牙是否打开,已打开返回true,否则,返回false listenUsingRfcommWithServiceRecord(String name,UUID uuid):根据名称,UUID创建并返回BluetoothServerSocket,这是创建BluetoothSocket服务器端的第一步 startDiscovery():开始搜索,这是搜索的第一步 4.使设备能够被搜索: 首先判断初始状态: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 //本机设备被扫描初始状态bufen if (mBluetoothAdapter.getScanMode()== 23 ){ //开启状态设置bufen //开启操作 //弹窗 //Intentintent=newIntent( //BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); ////设置蓝牙可见性的时间,方法本身规定最多可见300秒 //intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,BLUE_TIME); //startActivityForResult(intent,SEARCH_BLUE); //不弹窗 SettingblueUtils.setDiscoverableTimeout( 120 ); shezhi_blue_research( true ); new Handler().postDelayed( new Runnable(){ @Override public void run(){ closeDiscoverableTimeout(); shezhi_blue_research( false ); } }, 120 * 1000 ); } else if (mBluetoothAdapter.getScanMode()== 21 ){ //关闭状态设置bufen closeDiscoverableTimeout(); } 然后设置代码部分: 开启: 1 2 3 4 5 6 7 8 9 10 11 12 13 public static void setDiscoverableTimeout( int timeout){ BluetoothAdapteradapter=BluetoothAdapter.getDefaultAdapter(); try { MethodsetDiscoverableTimeout=BluetoothAdapter. class .getMethod( "setDiscoverableTimeout" , int . class ); setDiscoverableTimeout.setAccessible( true ); MethodsetScanMode=BluetoothAdapter. class .getMethod( "setScanMode" , int . class , int . class ); setScanMode.setAccessible( true ); setDiscoverableTimeout.invoke(adapter,timeout); setScanMode.invoke(adapter,BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE,timeout); } catch (Exceptione){ e.printStackTrace(); } } 关闭: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public static void closeDiscoverableTimeout(){ BluetoothAdapteradapter=BluetoothAdapter.getDefaultAdapter(); try { MethodsetDiscoverableTimeout=BluetoothAdapter. class .getMethod( "setDiscoverableTimeout" , int . class ); setDiscoverableTimeout.setAccessible( true ); MethodsetScanMode=BluetoothAdapter. class .getMethod( "setScanMode" , int . class , int . class ); setScanMode.setAccessible( true ); setDiscoverableTimeout.invoke(adapter, 1 ); setScanMode.invoke(adapter,BluetoothAdapter.SCAN_MODE_CONNECTABLE, 1 ); } catch (Exceptione){ e.printStackTrace(); } } 5.抓取蓝牙列表List的方法: 1 2 3 4 5 6 //获取所有已经绑定的蓝牙设备 Set<BluetoothDevice>devices=mBluetoothAdapter.getBondedDevices(); List<BluetoothDevice>mList1= new ArrayList<>(); if (devices.size()> 0 ){ mList1= new ArrayList<>(devices); } 6.注册搜索蓝牙receiver: 1 2 3 4 5 6 IntentFiltermFilter= new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); mFilter.addAction(BluetoothDevice.ACTION_FOUND); mFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); mFilter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); mFilter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED); registerReceiver(mReceiver,mFilter); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 private BroadcastReceivermReceiver= new BroadcastReceiver(){ @Override public void onReceive(Contextcontext,Intentintent){ //TODOAuto-generatedmethodstub Stringaction=intent.getAction(); //获得已经搜索到的蓝牙设备 if (action.equals(BluetoothDevice.ACTION_FOUND)){ BluetoothDevicedevice11=intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //搜索到的不是已经绑定的蓝牙设备 if (!is_shebei_open){ receiver1(device11); } else { receiver11(device11); } /**当绑定的状态改变时*/ } else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)){ BluetoothDevicedevice22=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); receiver2(device22,MainActivity_BlueTooth. this ); } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){ receiver3(); } else if (action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)){ int state=intent.getIntExtra(BluetoothA2dp.EXTRA_STATE,BluetoothA2dp.STATE_DISCONNECTED); receiver4(state); } else if (action.equals(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED)){ int state=intent.getIntExtra(BluetoothA2dp.EXTRA_STATE,BluetoothA2dp.STATE_NOT_PLAYING); receiver5(state); } } }; 最后说一下,多连接目前还没有解决,个人试了很多方法,大家如果有方法告诉我,谢谢~ 本文转自 吴雨声 51CTO博客,原文链接:http://blog.51cto.com/liangxiao/1905941,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册