蓝牙问题我这边也遇到了,怎么判断与当前设备为连接状态?
发布网友
发布时间:2022-05-11 09:39
我来回答
共2个回答
热心网友
时间:2023-10-10 12:10
.h文件
#import <Foundation/Foundation.h>
@interface TestConn : NSObject
+ (int)networkState;
@end
.m文件
#import "TestConn.h"
#import <SystemConfiguration/SystemConfiguration.h> // 需要事先导入SystemConfiguration.framework
#import <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
@implementation TestConn
+ (int)networkState
{
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// 以下objc相关函数、类型需要添加System Configuration 框架
// 用0.0.0.0来判断本机网络状态
SCNetworkReachabilityRef defaultRouteReachability =
SCNetworkReachabilityCreateWithAddress(NULL, (struct
sockaddr*)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags
= SCNetworkReachabilityGetFlags(defaultRouteReachability,&flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags)
{
return -1;
}
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
return (isReachable && !needsConnection) ? 1 : 0;
}
@end追问哥们,我现在搞的是android, 如果有android蓝牙相关的demo请留一个, 这个项目对蓝牙各项要求很高,
热心网友
时间:2023-10-10 12:10
详细什么情况呢?