1.倒入框架 蓝牙
import CoreBluetooth
2.遵守协议
class AppDelegate: UIResponder, UIApplicationDelegate,CBCentralManagerDelegate,
CBPeripheralDelegate {
3.声明变量和初始化
var centralManager:CBCentralManager!
var selectedPeripheral:CBPeripheral?
var deviceArray:NSMutableArray = []
var characterFFB0:CBCharacteristic?
self.deviceArray = NSMutableArray.init()
self.centralManager = CBCentralManager.init(delegate:self, queue: nil, options: [CBCentralManagerOptionShowPowerAlertKey:(true)])
4.方法的调用
func addView() {
}
self.addView()
5.通知的发送和接收
<!--发送-->
NotificationCenter.default.post(Notification.init(name: Notification.Name(rawValue: "readMac")))
<!--接收-->
NotificationCenter.default.addObserver(self, selector: #selector(readMac), name: NSNotification.Name(rawValue: "readMac"), object: nil)
<!--通知的方法-->
@objc func readMac(){
self.selectedPeripheral?.readValue(for:self.characterFFB3!)
}
6.byte数组转成Data
var byte:[UInt8] = [0,0,0,0,0,0]
byte[0] = UInt8(year)
byte[1] = UInt8(month)
byte[2] = UInt8(day)
byte[3] = UInt8(hour)
byte[4] = UInt8(min)
byte[5] = UInt8(sec)
let data = Data.init(bytes:byte)
7.系统版本的判断 和 switch case方法
if #available(iOS 10.0, *) {
switch central.state{
case .poweredOn:
print("powerOn")
case .unknown:
print("unknown")
case .resetting:
print("restting")
case .unsupported:
print("unsupported")
case .unauthorized:
print("unauthorized")
case .poweredOff:
print("powerOff")
}
}
8.error 判断
if (error != nil) {
print("\(error.debugDescription)")
}
9.字符串判断
<!--同 isEqualToString-->
if uuidStr == "FFB4"
10.声明常量
let screenWidth = UIScreen.main.bounds.size.width;
let screenHeight = UIScreen.main.bounds.size.height;
11.自定义collectionView
func addCollectionView() {
let flowLayout = UICollectionViewFlowLayout.init();
flowLayout.itemSize = CGSize.init(width: (screenWidth-20 * 2-10 * 2)/3.0, height: (screenWidth-20 * 2-10 * 2)/3.0)
flowLayout.minimumInteritemSpacing = 10;
flowLayout.minimumLineSpacing = 10;
flowLayout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
let collectionView = UICollectionView.init(frame: CGRect.init(x: 20, y:self.deviceLab.frame.maxY+10, width:screenWidth-20*2, height:screenHeight-self.deviceLab.frame.maxY-10), collectionViewLayout: flowLayout)
collectionView.delegate = self;
collectionView.dataSource = self;
collectionView.register(MainCell.self, forCellWithReuseIdentifier: "reusedID")
collectionView.backgroundColor = UIColor.white
collectionView.showsVerticalScrollIndicator = false;
collectionView.showsHorizontalScrollIndicator = false;
self.view.addSubview(collectionView)
}
12.自定义collectionViewCell
import UIKit
class MainCell: UICollectionViewCell {
var imgView:UIImageView!
var titelLab:UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupUI() { //无返回值的参数
imgView = UIImageView.init();
titelLab = UILabel.init();
titelLab.font = UIFont.systemFont(ofSize: 14);
titelLab.textAlignment = NSTextAlignment.center;
titelLab.textColor = UIColor.black;
self.addSubview(imgView)
self.addSubview(titelLab)
}
override func layoutSubviews() {
super.layoutSubviews()
let frame:CGRect = self.frame
self.imgView.frame = CGRect.init(x: 0, y: 0, width:frame.size.width , height: frame.size.height-20)
self.titelLab.frame = CGRect.init(x: 0, y: frame.size.height-20, width: frame.size.width, height: 20)
}
}
13.添加tableView
func addTabView() {
self.tableView = UITableView.init(frame:CGRect.init(x: 0, y:0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height), style: UITableViewStyle.plain)
self.tableView?.delegate = self;
self.tableView?.dataSource = self;
self.tableView?.rowHeight = 68;
self.tableView?.showsVerticalScrollIndicator = false;
self.tableView?.tableFooterView = UIView.init();
self.tableView?.tableHeaderView = UIView.init()
self.tableView?.register(ListCell.self, forCellReuseIdentifier: "Cell")
self.view.addSubview(self.tableView!);
}
14.自定义tableViewCell
import UIKit
class ListCell: UITableViewCell {
var nameLab:UILabel!
var rssiLab:UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier);
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupUI() {
nameLab = UILabel.init();
nameLab.font = UIFont.systemFont(ofSize: 14)
nameLab.textAlignment = NSTextAlignment.left;
nameLab.textColor = UIColor.black;
addSubview(self.nameLab)
rssiLab = UILabel.init();
rssiLab.font = UIFont.systemFont(ofSize: 13)
nameLab.textAlignment = NSTextAlignment.center;
nameLab.textColor = UIColor.black;
self.addSubview(self.rssiLab)
}
override func layoutSubviews() {
super.layoutSubviews()
self.nameLab.frame = CGRect.init(x: 20, y: 20, width: 100, height: 28);
self.rssiLab.frame = CGRect.init(x: 140, y: 20, width: 100, height: 28);
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
以上只是我自己的记录,可能存在问题。